在主题文件的function.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);
echo '<p>友情链接:';
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> ';
}
echo '</p>';
}
}
在WordPress 后台 > 设置 > 阅读 界面,就有一个友情链接添加的输入框。按照 链接 |标题 的方式输入所有的友情链接:多个换行即可
然后在前台需要显示的地方添加
<?php
if (function_exists('wpjam_blogroll')) { wpjam_blogroll(); }
?>
如果只想在首页显示的话
<?php
if (function_exists('wpjam_blogroll')&& (is_home()||is_front_page())) { wpjam_blogroll(); }
?>
如果要在新窗口打开的话,修改 wpjam_blogrol 函数里面的这一行就可以了
修改为
echo '<a target="_blank" href="'.trim($wpjam_blogroll[0]).'" title="'.esc_attr(trim($wpjam_blogroll[1])).'">'.trim($wpjam_blogroll[1]).'</a> ';
前台展示效果(当然需要自己调整展示的位置以及字体样式等)