wordpress笔记之自定义友链页面

在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->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 .= '';

$output .= $link_before;

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

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

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;
}

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' => $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\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笔记之读者墙+留言板
  •  标签: 折腾, 笔记, wordpress, 友链

    作者  :  sailor

    仅仅看明白了还不够,能把别人给讲明白了才算及格。


    1. walfred
      1#
      walfred

      欢迎交换友链

      September 27th, 2011 at 10:37 pm 回复
        1. sailor
          sailor

          那就换一个呗~[face:daxiao][face:daxiao]

          September 28th, 2011 at 01:21 pm 回复
    2. 寒意
      2#
      寒意

      像这样比较多的css就不要写在主css文件上了。另做一个css文件只添加到这个页面上比较好~

      September 28th, 2011 at 10:19 pm 回复
        1. sailor
          sailor

          恩,这的确是个问题,我去尝试一下,最近有时间就折腾一下

          October 20th, 2011 at 03:39 am 回复


    最新文章