闻心阁

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

wordpress笔记之自定义友链页面

2011-09-27 约 4 分钟读完 搬砖秘籍

在wordpress中友链是必不可少的,但是随着时间的增加,也要分个首页和内页友链了,于是我就想个给自己的友链单独添加一个页面。当然我自己是不会写的,还是要修改高手的代码。

这个方法同样来自万戈,同样的方法,先复制一个page.php,然后按照下面的方法修改。

 

新建一个页面模板,打开所用主题的 page.php 文件,在文件最顶端加上以下声明代码:

<?php
/*
Template Name: Links Page Template PAGE
*/
?>

再找到如下代码:

<div class="entry">
<?php the_content(); ?>
</div><!--/entry -->

修改为:

<div class="entry">
<?php wp_list_bookmarks('categorize=1&category=XX,YY,ZZ&
category_orderby=id&before=<li>&after=</li>&show_images=0&
show_description=1&orderby=name&title_before=<h3>&title_after=</h3>'); ?>
</div><!--/entry —>

XX YY ZZ指你的友链分类。

CSS美化:

/*---------------------Links Page-----------------------*/
#mainlink {
clear: both;
margin: 0 40px;
width: 900px; /* Prevents IE6 miscalculation */
background: #fff;
}
.linkpage ul {
padding: 5px 6px;
list-style-type: none;
overflow:auto
}
* html .linkpage ul{ height:1%;}
.linkpage ul li {
color: #333;
margin-bottom: 5px;
font-size: 12px;
}
.linkpage ul li ul li {
float: left;
width: 141px;
text-align: center;
margin: 3px 3px;
line-height: 180%;
background-color: #FFFFFF;
border: 1px solid #dadada;
}
.linkpage ul li ul li a {
color: gray;
display: block;
}
.linkpage ul li ul li a:hover {
background-color: gray;
color: #FFFFFF;
font-weight:bold;
}
.linkpost {
display:inline;
float:left;
margin:0 20px;
padding:0 5px;
width:920px;
}
#linkcontent {
margin-top:8px;
width:970px;
float:left;
}

显示友链图标

这个要自己在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' => '

', 'after' => '', '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-&gt;recently_updated) )
		$bookmark-&gt;recently_updated = false;
	$output .= $before;
	if ( $show_updated &amp;&amp; $bookmark-&gt;recently_updated )
		$output .= get_option('links_recently_updated_prepend');

	$the_link = '#';
	if ( !empty($bookmark-&gt;link_url) )
		$the_link = clean_url($bookmark-&gt;link_url);

	$rel = ' rel="external';
	if ($nofollow)
		$rel .= ' nofollow';
	if ( '' != $bookmark-&gt;link_rel )
		$rel .= ' ' . $bookmark-&gt;link_rel;
	$rel .= '"';

	$desc = attribute_escape(sanitize_bookmark_field('link_description', $bookmark-&gt;link_description, $bookmark-&gt;link_id, 'display'));
	$name = attribute_escape(sanitize_bookmark_field('link_name', $bookmark-&gt;link_name, $bookmark-&gt;link_id, 'display'));
	$title = $desc;

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

	if ( '' != $title )
		$title = ' title="' . $title . '"';

	$alt = ' alt="' . $name . '"';

	$target = $bookmark-&gt;link_target;
	if ( '' != $target )
		$target = ' target="' . $target . '"';

	$output .= '';

	$output .= $link_before;

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

	$output .= $name;
	$output .= $link_after;
	$output .= '';

	if ( $show_updated &amp;&amp; $bookmark-&gt;recently_updated )
		$output .= get_option('links_recently_updated_append');

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

	if ($show_rating) {
		$output .= $between . sanitize_bookmark_field('link_rating', $bookmark-&gt;link_rating, $bookmark-&gt;link_id, 'display');
	}

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

return $output;

}

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’ => ‘

’, ’title_after’ => ‘

’, ‘category_orderby’ => ’name’, ‘category_order’ => ‘ASC’, ‘class’ => ’linkcat’, ‘category_before’ => ‘

’, ‘category_after’ => ‘’,’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' =&gt; $category_name, 'include' =&gt; $category, 'exclude' =&gt; $exclude_category, 'orderby' =&gt; $category_orderby, 'order' =&gt; $category_order, 'hierarchical' =&gt; 0));

	foreach ( (array) $cats as $cat ) {
		$params = array_merge($r, array('category'=&gt;$cat-&gt;term_id));
		$bookmarks = get_bookmarks($params);
		if ( empty($bookmarks) )
			continue;
		$output .= str_replace(array('%id', '%class'), array("linkcat-$cat-&gt;term_id", $class), $category_before);
		$catname = apply_filters( "link_category", $cat-&gt;name );
		$output .= "$title_before$catname$title_after\n\t\n";
		$output .= my_bookmarks($bookmarks, $r);
		$output .= "\n\t\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\n";
			$output .= my_bookmarks($bookmarks, $r);
			$output .= "\n\t\n$category_after\n";
		} else {
			$output .= my_bookmarks($bookmarks, $r);
		}
	}
}

$output = apply_filters( 'wp_list_bookmarks', $output );

if ( !$echo )
	return $output;
echo $output;

} //——————-end google favicon—————————–

也就这些,具体效果根据自己的博客主题风格去搞就好~

折腾笔记系列:

  • wordpress笔记之侧边栏读者墙
  • wordpress笔记之不同页面不同侧边栏
  • wordpress笔记之头像缓存
  • wordpress笔记之评论框背景
  • wordpress笔记之评论表情
  • wordpress笔记之相关文章
  • wordpress笔记之版权信息
  • wordpress笔记之首页NEW提示
  • wordpress笔记之上下滑动条
  • wordpress笔记之友链两栏+排序
  • wordpress笔记之热门随机最新文章
  • wordpress笔记之取消自动保存和修订功能
  • wordpress笔记之读者墙+留言板