给wordpress增加友情链接页面及其美化的方法

2014年8月12日 / 网站源码 / 12条 / 5,464次

自从换成了唐杰的主题,发现这主题真的还不错,就是发现没有页面模版,比如友情链接、归档之类的都没有,所以一直很纳闷,网上搜索了许多的代码也没有用,最后整合其他主题的友情链接才成功建立比较满意的友情链接,但是由于google屏蔽问题,所以友情链接前面的小图标加载有点慢,但是还是算很完美了。想建立此页面,主要是不想在首页显示许多连接,因为很多没有相互添加的连接会降低网站权重,所以就需要在首页只是显示需要显示的友情链接的分类,其他的加到友情链接的页面去。此时就需要新建一个友情链接的页面在解决,以下是添加友情链接的方法和美化方法。

1、首选新建友情链接页面保存为links.php传到相应主题目录下:

  1. <?php
  2. /* Template Name: 友情链接 */
  3. ?>
  4. < ?php get_header();
  5. ?>
  6. < div id="link">
  7. < ?php if (have_posts()) : while (have_posts()) : the_post();
  8. ?>
  9. < div id="links">< h2>申请链接条件:<a target="_blank" href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=TiIrICk4KyAOPz9gLSEj" style="text-decoration:none;"><img src="http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_01.png"/></a>< /h2>< p align="left">如有意与本站做链接,请先加上本站链接后留言,我会在最快时间加上贵站。 </br>您所提交的链接会显示在这里,本站会选择部分链接在首页面显示。 </br>本站不要求访问量,什么PR多少都无所谓,甚至是新站都可以,但最重要的是持久性和原创性,所以您的博客是否有访问量不是本站考虑的重点,能够坚持并经常更新的原创日志才是真正的博客。  </br>在您认可以上的文字后,还有一点重要的,就是您的站点如果链接失效或者有些内容并不适合网络文明道德(国家法律说不清楚),本站将在不通知您的情况下删除..... </p>< p align="left">
  10. </p>< p align="left" style="color: #FF0000">本站链接信息:</p>< p align="left"> 博客名称:第九部落</p>< p align="left">博客地址:
  11. <a href="https://long.ge" target="_blank">https://long.ge</a>&nbsp;&nbsp;或&nbsp;&nbsp;<a href="https://long.ge" target="_blank">https://long.ge</a></p>< p align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ICO&nbsp;地址:https://long.ge/favicon.ico &nbsp; </p>< p align="left"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<IMG SRC="https://long.ge/favicon.ico" WIDTH=36 HEIGHT=36 title="第九部落" ALT="第九部落" border="0"></p>< /div>< div id="fengexian"></div>< div class="linkpage"><ul>< ?php my_list_bookmarks('categorize=1& category_orderby=id&before=<li>&after=</li>&show_images=1& show_description=1&orderby=name&title_before=<h3>&title_after=</h3>'); ?>< /ul>< /div>< ?php endwhileelse: ?>< ?php endif; ?>< /div>< div id="comments"><?php comments_template(); ?></div>< ?php get_footer(); ?>

由于上面添加有本网站友情链接的文字说明部分,可以修改为自己网站的信息。
2、在functions.php下添加:

  1. function my_bookmarks($bookmarks$args = '' ) {
  2.     $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 );
  3.     $r = wp_parse_args( $args$defaults );
  4.     extract( $r, EXTR_SKIP );
  5.     $output = '';
  6.     // Blank string to start with.
  7.     foreach ( (array$bookmarks as $bookmark ) {
  8.         if ( !isset($bookmark->recently_updated) ) $bookmark->recently_updated = false;
  9.         $output .= $before;
  10.         if ( $show_updated && $bookmark->recently_updated ) $output .= get_option('links_recently_updated_prepend');
  11.         $the_link = '#';
  12.         if ( !emptyempty($bookmark->link_url) ) $the_link = clean_url($bookmark->link_url);
  13.         $rel = ' rel="external';
  14.         if ($nofollow) $rel .= ' nofollow';
  15.         if ( '' != $bookmark->link_rel ) $rel .= ' ' . $bookmark->link_rel;
  16.         $rel .= '"';
  17.         $desc = attribute_escape(sanitize_bookmark_field('link_description', $bookmark->link_description, $bookmark->link_id, 'display'));
  18.         $name = attribute_escape(sanitize_bookmark_field('link_name', $bookmark->link_name, $bookmark->link_id, 'display'));
  19.         $title = $desc;
  20.         if ( $show_updated ) if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
  21.             $title .= ' (';
  22.             $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * 3600)));
  23.             $title .= ')';
  24.         }
  25.         if ( '' != $title ) $title = ' title="' . $title . '"';
  26.         $alt = ' alt="' . $name . '"';
  27.         $target = $bookmark->link_target;
  28.         if ( '' != $target ) $target = ' target="' . $target . '"';
  29.         $output .= '<a href="' . $the_link . '"' . $rel . $title . $target. '>';
  30.         $output .= $link_before;
  31.         if ( $show_images ) {
  32.             if ( $bookmark->link_image != null) {
  33.                 if ( strpos($bookmark->link_image, 'http') !== false ) $output .= "<img src="$bookmark->link_image" $alt $title />"else // If it's a relative path
  34.                 $output .= "<img src="" . get_option('siteurl') . "$bookmark->link_image" $alt $title />";
  35.             } else {
  36.                 //否则显示网站的Favicon
  37.                 if (preg_match('/^(https?://)?([^/]+)/i',$the_link,$URI)) {
  38.                     //提取域名
  39.                     $domains = $URI[2];
  40.                 } else {
  41.                     //域名提取失败,显示默认小地球 
  42.                     $domains = "example.com";
  43.                 }
  44.                 $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 .= "$aftern"; } // end while 
  45.                 return $output;
  46.             }

3、在style.css里增加样式:

  1. /** 友情链接页面 **/
  2. #link {
  3.     padding:2px;
  4.     margin-bottom:10px;
  5.     background:#fff;
  6.     color:#444;
  7.     border:1px #ddd solid;
  8.     box-shadow:0 2px 10px #eee,inset 0 -2px 10px #eee;
  9.     -moz-border-radius:5px;
  10.     -webkit-border-radius:5px;
  11.     border-radius:5px 5px 5px 5px;
  12.     border-radius:5px;
  13. }
  14. #links {
  15.     padding:12px 12px 5px 20px;
  16. }
  17. #links h2 {
  18.     font-size:17px;
  19.     padding:0px 12px 15px 0px;
  20. }
  21. #fengexian {
  22.     background:url(images/fengexian.png) no-repeat center;
  23.     padding:30px 12px 30px 0px;
  24. }
  25. .linkpage ul {
  26.     list-style-type:none;
  27.     overflow:auto;
  28.     padding:0px 0px 12px 20px;
  29. }
  30. * html .linkpage ul {
  31.     height:1%;
  32. }
  33. .linkpage ul li {
  34.     color:#333333;
  35.     font-size:12px;
  36.     margin-bottom:5px;
  37. }
  38. .linkpage ul li h3 {
  39.     color:#333333;
  40.     font-size:15px;
  41.     margin-bottom:5px;
  42. }
  43. .linkpage ul li ul li {
  44.     background-color:#FFFFFF;
  45.     border:1px solid #DADADA;
  46.     float:left;
  47.     border-radius:5px 5px 5px 5px;
  48.     font-size:12px;
  49.     line-height:180%;
  50.     margin:3px;
  51.     text-align:left;
  52.     text-shadow:1px 1px 1px #CCC;
  53.     width:116px;
  54. }
  55. .linkpage ul li ul li a {
  56.     color:gray;
  57.     display:block;
  58. }
  59. .linkpage ul li ul li a:hover {
  60.     background-color:gray;
  61.     color:#FFFFFF;
  62.     font-weight:bold;
  63. }
  64. .linkpage IMG {
  65.     float:left;
  66.     height:16px;
  67.     padding:3px 4px 3px 3px;
  68.     width:16px;
  69. }

《给wordpress增加友情链接页面及其美化的方法》有12 条评论

  1. loverail说道:

    你好,看到你博客 不错,想你在博客里加广告,是否愿意合作,每月100,愿意的话联系我qq2310716987

  2. 涂涂研版说道:

    头像怎么也都看不见了。。 🙁

  3. 知道91博客说道:

    这个不错哟,呵呵 😛

  4. php程序员说道:

    友情链接直接使用了插件。。

  5. 沈军说道:

    方法实用,支持!

  6. 林三说道:

    手动添加这些页面,自成一体,挺好的!

  7. 大色网说道:

    wp挺不错的,但是不怎么会用,


大色网 留言