闻心阁

一蓑烟雨看苍生,半壶浊酒笑红尘

wordpress自定义友链页面

2012-03-04 约 4 分钟读完 搬砖秘籍

wordpress中友链是必不可少的,但是随着时间的增加,也要分个首页和内页友链了,于是我就想个给自己的友链单独添加一个页面。下面是我搜集的链接方法,这里做个记录。

新建一个页面模板,命名friends.php,打开所用主题的 page.php 文件,复制所有代码到friends.php中。在friends.php文件最顶端加上以下声明代码:

找到你friends.php中这个函数,在这个函数的后面加上下面的代码

<div class="linkpage">
<ul>
<?php my_list_bookmarks('categorize=1&
category_orderby=id&before=<li>&after=&show_images=1&
show_description=1&orderby=name&title_before=<h3>&title_after=</h3>'); ?>
</li></ul>

这里用到了两个函数,需要自己定义,把下面的两个函数代码复制到主题的functions.php,代码如下:

//--------------start google favicon--------------------------
function my_bookmarks($bookmarks, $args = '' ) {
$defaults = array(
'show_updated' => 0, 'show_description' => 0,
'show_images' => 1, 'show_name' => 0,
'before' => '<li>', 'after' => '</li>', 'between' => "\n",
'show_rating' => 0, 'link_before' => '', 'link_after' => '','nofollow' =>0
);

$r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP );

$output = ‘’; // Blank string to start with.

foreach ( (array) $bookmarks as $bookmark ) { if ( !isset($bookmark->recently_updated) ) $bookmark->recently_updated = false; $output .= $before; if ( $show_updated && $bookmark->recently_updated ) $output .= get_option(’links_recently_updated_prepend’);

$the_link = ‘#’; if ( !empty($bookmark->link_url) ) $the_link = clean_url($bookmark->link_url);

$rel = ’ rel=“external’; if ($nofollow) $rel .= ’ nofollow’; if ( ’’ != $bookmark->link_rel ) $rel .= ’ ’ . $bookmark->link_rel; $rel .= ‘”’;

$desc = attribute_escape(sanitize_bookmark_field(’link_description’, $bookmark->link_description, $bookmark->link_id, ‘display’)); $name = attribute_escape(sanitize_bookmark_field(’link_name’, $bookmark->link_name, $bookmark->link_id, ‘display’)); $title = $desc;

if ( $show_updated ) if ( ‘00’ != substr($bookmark->link_updated_f, 0, 2) ) { $title .= ’ (’; $title .= sprintf(__(‘Last updated: %s’), date(get_option(’links_updated_date_format’), $bookmark->link_updated_f + (get_option(‘gmt_offset’) * 3600))); $title .= ‘)’; }

if ( ’’ != $title ) $title = ’ title="’ . $title . ‘"’;

$alt = ’ alt="’ . $name . ‘"’;

$target = $bookmark->link_target; if ( ’’ != $target ) $target = ’ target="’ . $target . ‘"’;

$output .= ‘<a href="’ . $the_link . ‘"’ . $rel . $title . $target. ‘>’;

$output .= $link_before;

if ( $show_images ) { if ( $bookmark->link_image != null) { if ( strpos($bookmark->link_image, ‘http’) !== false ) $output .= “<img src="$bookmark->link_image" $alt $title />”; else // If it’s a relative path $output .= “<img src="” . get_option(‘siteurl’) . “$bookmark->link_image" $alt $title />”; } else {//否则显示网站的Favicon if (preg_match(’/^(https?://)?([^/]+)/i’,$the_link,$URI)) {//提取域名 $domains = $URI[2]; }else{//域名提取失败,显示默认小地球 $domains = “example.com”; } $output .= “<img src="http://www.google.com/s2/favicons?domain=$domains" $alt $title />”; } }

$output .= $name; $output .= $link_after; $output .= ‘</a>’;

if ( $show_updated && $bookmark->recently_updated ) $output .= get_option(’links_recently_updated_append’);

if ( $show_description && ’’ != $desc ) $output .= $between . $desc;

if ($show_rating) { $output .= $between . sanitize_bookmark_field(’link_rating’, $bookmark->link_rating, $bookmark->link_id, ‘display’); }

$output .= “$after\n”; } // end while

return $output; } //–end google———————————— //—friend page——————————— function my_list_bookmarks($args = ‘’) { $defaults = array( ‘orderby’ => ’name’, ‘order’ => ‘ASC’, ’limit’ => -1, ‘category’ => ‘’, ’exclude_category’ => ‘’, ‘category_name’ => ‘’, ‘hide_invisible’ => 1, ‘show_updated’ => 0, ’echo’ => 1, ‘categorize’ => 1, ’title_li’ => __(‘Bookmarks’), ’title_before’ => ‘<h2>’, ’title_after’ => ‘</h2>’, ‘category_orderby’ => ’name’, ‘category_order’ => ‘ASC’, ‘class’ => ’linkcat’, ‘category_before’ => ‘<li id="%id" class="%class">’, ‘category_after’ => ‘</li>’,’nofollow’ => 0 );

$r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP );

$output = ‘’;

if ( $categorize ) { //Split the bookmarks into ul’s for each category $cats = get_terms(’link_category’, array(’name__like’ => $category_name, ‘include’ => $category, ’exclude’ => $exclude_category, ‘orderby’ => $category_orderby, ‘order’ => $category_order, ‘hierarchical’ => 0));

foreach ( (array) $cats as $cat ) { $params = array_merge($r, array(‘category’=>$cat->term_id)); $bookmarks = get_bookmarks($params); if ( empty($bookmarks) ) continue; $output .= str_replace(array(’%id’, ‘%class’), array(“linkcat-$cat->term_id”, $class), $category_before); $catname = apply_filters( “link_category”, $cat->name ); $output .= “$title_before$catname$title_after\n\t<ul class=‘xoxo blogroll’>\n”; $output .= my_bookmarks($bookmarks, $r); $output .= “\n\t</ul>\n$category_after\n”; } } else { //output one single list using title_li for the title $bookmarks = get_bookmarks($r);

if ( !empty($bookmarks) ) { if ( !empty( $title_li ) ){ $output .= str_replace(array(’%id’, ‘%class’), array(“linkcat-$category”, $class), $category_before); $output .= “$title_before$title_li$title_after\n\t<ul class=‘xoxo blogroll’>\n”; $output .= my_bookmarks($bookmarks, $r); $output .= “\n\t</ul>\n$category_after\n”; } else { $output .= my_bookmarks($bookmarks, $r); } } }

$output = apply_filters( ‘wp_list_bookmarks’, $output );

if ( !$echo ) return $output; echo $output; } //—end friend—-

最后就是CSS美化了,这个就是仁者见仁智者见智的事情了,附上我的CSS代码

/*---------------------Links Page-----------------------*/
.linkpage h3 {line-height:20px;margin:0;}
.linkpage ul {margin:0 0 0 40px;overflow:auto;}
.xoxo li a img{float:left;background: white;margin: 0;padding: 6px;border: none;border-radius: 1px;}
.xoxo li a img:hover{background:#A8A8A8;border:none;}
.linkpage ul li ul{margin:0;}
* html .linkpage ul {height:1%;}
.linkpage ul li {color: #333;margin-bottom: 5px;font-size: 12px;}
.linkpage ul li ul li {float: left;width: 120px;height: 25px;text-align: center;margin: 4px;line-height: 180%;background-color: #FFFFFF;border: 1px solid #dadada;overflow: hidden;border-radius: 5px;}
.linkpage ul li ul li a {text-decoration: none;color: gray; display: block;}
.linkpage ul li ul li:hover {background:#A8A8A8;}

效果请看本站的友链页面