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 rel="nofollow noopener noreferrer" 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 rel="nofollow noopener noreferrer" href="'.trim($wpjam_blogroll[0]).'" title="'.esc_attr(trim($wpjam_blogroll[1])).'">'.trim($wpjam_blogroll[1]).'</a>'; } } } ?>

然后就可以在 WordPress 后台 > 设置 > 阅读 界面,就有一个友情链接添加的输入框。按照 链接 |标题 的方式输入所有的友情链接:

最后在需要显示友情链接的模板文件的适当位置,使用下面的代码进行调用:

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" rel="nofollow noopener noreferrer" href="'.trim($wpjam_blogroll[0]).'" title="'.esc_attr(trim($wpjam_blogroll[1])).'">'.trim($wpjam_blogroll[1]).'</a>';

echo ' | <a target="_blank" rel="nofollow noopener noreferrer" 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();?>

到此这篇关于WordPress快速添加友情链接就介绍到这了。如果你的工具只有一柄铁锤,你就可能认为所有的问题都是铁钉。更多相关WordPress快速添加友情链接内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?

WordPress安装在主机空间的什么目录里面?根目录在哪里?