本文实例讲述了WordPress快速添加友情链接功能的方法。分享给大家供大家参考。具体方法分析如下:
WordPress系统默认下是没有友情链接功能的,为了网站更好的优化我们需对它加个友情连接功能,下面我把我在wordpress中增加友情链接具体方法分享给大家.
WordPress 在 3.5 己经隐藏了,我们可以通过下面的代码实现快速添加友情链接,实例代码如下:
- <?php
- /*
- Plugin Name: WPJAM Blogroll
- Description: 快速添加友情链接
- Version: 0.1
- Author: Denis
- */
- 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>';
- }
- }
- }
- ?>
首先将上面的代码复制到当前主题的 functions.php 文件中,或者单独作为一个插件激活,然后就可以在 WordPress 后台 > 设置 > 阅读 界面,就有一个友情链接添加的输入框,按照 链接 |标题 的方式输入所有的友情链接.
方法二:将下面的代码添加到当前主题的 functions.php 文件中,或者另存为单独的php文件,上传到插件目录激活,代码如下:
- <?php
- 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建站有所帮助。