WordPress 快速添加友情链接的另类方法

首先将下面的代码复制到当前主题的 functions.php 文件中,或者单独作为一个插件激活。

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

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

最后在主题输出友情链接的地方通过下面的模板函数调用:

  1. <?php wpjam_blogroll();?>

版权声明:我爱水煮鱼


发布日期:

所属分类: Wordpress 综合 标签: