WordPress快速添加友情鏈接

WordPress 3.5 默認隱藏瞭友情鏈接管理功能,雖然可以 恢復鏈接管理功能 ,但是自帶的鏈接管理仍舊太復雜, 下面分享下 @我愛水煮魚  的方法。

將下面的代碼添加到當前主題的 functions.php 文件中,或者另存為單獨的php文件,上傳到外掛目錄激活:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
/*
Plugin Name: WPJAM Blogroll
Plugin URI: http://blog.wpjam.com/m/wpjam-blogroll/
Description: 快速添加友情鏈接
Version: 0.1
Author: Denis
Author URI: http://blog.wpjam.com/
*/
add_action('admin_init', 'wpjam_blogroll_settings_api_init');
function wpjam_blogroll_settings_api_init() {
    add_settings_field('wpjam_blogroll_setting', '友情鏈接', 'wpjam_blogroll_setting_callback_function', 'reading');
    register_setting('reading','wpjam_blogroll_setting');
}
 
function wpjam_blogroll_setting_callback_function() {
    echo '<textarea name="wpjam_blogroll_setting" rows="10" cols="50" id="wpjam_blogroll_setting" class="large-text code">' . get_option('wpjam_blogroll_setting') . '</textarea>';
}
 
function wpjam_blogroll(){
    $wpjam_blogroll_setting =  get_option('wpjam_blogroll_setting');
    if($wpjam_blogroll_setting){
        $wpjam_blogrolls = explode("\n", $wpjam_blogroll_setting);
        foreach ($wpjam_blogrolls as $wpjam_blogroll) {
            $wpjam_blogroll = explode("|", $wpjam_blogroll );
            echo ' | <a href="'.trim($wpjam_blogroll[0]).'" title="'.esc_attr(trim($wpjam_blogroll[1])).'">'.trim($wpjam_blogroll[1]).'</a>';
        }
    }
}
 
?>

<?php
/*
Plugin Name: WPJAM Blogroll
Plugin URI: http://blog.wpjam.com/m/wpjam-blogroll/
Description: 快速添加友情鏈接
Version: 0.1
Author: Denis
Author URI: http://blog.wpjam.com/
*/
add_action(‘admin_init’, ‘wpjam_blogroll_settings_api_init’);
function wpjam_blogroll_settings_api_init() {
add_settings_field(‘wpjam_blogroll_setting’, ‘友情鏈接’, ‘wpjam_blogroll_setting_callback_function’, ‘reading’);
register_setting(‘reading’,’wpjam_blogroll_setting’);
}
function wpjam_blogroll_setting_callback_function() {
echo ‘<textarea name="wpjam_blogroll_setting" rows="10" cols="50" id="wpjam_blogroll_setting" class="large-text code">’ . get_option(‘wpjam_blogroll_setting’) . ‘</textarea>’;
} function wpjam_blogroll(){
$wpjam_blogroll_setting = get_option(‘wpjam_blogroll_setting’);
if($wpjam_blogroll_setting){
$wpjam_blogrolls = explode("\n", $wpjam_blogroll_setting);
foreach ($wpjam_blogrolls as $wpjam_blogroll) {
$wpjam_blogroll = explode("|", $wpjam_blogroll );
echo ‘ | <a href="’.trim($wpjam_blogroll[0]).’" title="’.esc_attr(trim($wpjam_blogroll[1])).’">’.trim($wpjam_blogroll[1]).'</a>’;
}
}
} ?>

然後就可以在 WordPress 後臺 > 設置 > 閱讀 界面,就有一個友情鏈接添加的輸入框。按照 鏈接 |標題 的方式輸入所有的友情鏈接:

WordPress-Quick-Add-links-wpdaxue_com

最後在需要顯示友情鏈接的樣版文件的適當位置,使用下面的代碼進行調用:

1
<?php if (function_exists(wpjam_blogroll)) wpjam_blogroll();?>

<?php if (function_exists(wpjam_blogroll)) wpjam_blogroll();?>

參考資料:http://blog.wpjam.com/m/wpjam-blogroll/

1.新窗口打開鏈接

新窗口打開,修改最上面的代碼的26行為:

1
echo ' | <a target="_blank" href="'.trim($wpjam_blogroll[0]).'" title="'.esc_attr(trim($wpjam_blogroll[1])).'">'.trim($wpjam_blogroll[1]).'</a>';

echo ‘ | <a target="_blank" href="’.trim($wpjam_blogroll[0]).’" title="’.esc_attr(trim($wpjam_blogroll[1])).’">’.trim($wpjam_blogroll[1]).'</a>’;

2.隻在首頁顯示

首頁要看你具體添加到哪個文件瞭,你可以通過下面的代碼調用試試:

1
<?php if (function_exists(wpjam_blogroll) && (is_home()||is_front_page()) wpjam_blogroll();?>

<?php if (function_exists(wpjam_blogroll) && (is_home()||is_front_page()) wpjam_blogroll();?>

發佈留言