Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1811405
  • 博文数量: 354
  • 博客积分: 11021
  • 博客等级: 上将
  • 技术积分: 4048
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-24 10:04
文章存档

2020年(1)

2018年(1)

2016年(1)

2015年(2)

2014年(4)

2013年(4)

2012年(12)

2011年(14)

2010年(37)

2009年(34)

2008年(22)

2007年(68)

2006年(102)

2005年(29)

2004年(21)

分类: 系统运维

2012-06-14 22:49:52

网页制作Webjx文章简介:在single文章页使用相关文章功能的好处是显而易见的,可以增加网站的粘度的同时,更多地是更方便地为用户列出了他可能关心的内容。

在single文章页使用相关文章功能的好处是显而易见的,可以增加网站的粘度的同时,更多地是更方便地为用户列出了他可能关心的内容。一般情况下我们是使用水煮鱼的WordPress Related Posts插件来实现的,那么,在尽量节约插件的使用数量的前提下,我们还可以手动添加代码来实现。

1,相关文章非插件实现方法

点击(此处)折叠或打开

  1. <?php
  2. //for use in the loop, list 5 post titles related to first tag on current post
  3. $tags = wp_get_post_tags($post->ID);
  4. if ($tags) {
  5.  echo 'Related Posts';
  6.  $first_tag = $tags[0]->term_id;
  7.  $args=array(
  8.  'tag__in' => array($first_tag),
  9.  'post__not_in' => array($post->ID),
  10.  'showposts'=>5,
  11.  'caller_get_posts'=>1
  12.  );
  13.  $my_query = new WP_Query($args);
  14.  if( $my_query->have_posts() ) {
  15.  while ($my_query->have_posts()) : $my_query->the_post(); ?>
  16.  <p><a href="" rel="bookmark" title="Permanent Link to "><?php the_title(); ?></a></p>
  17.  <?php
  18.  endwhile;
  19.  }
  20. }
  21. ?>

2,创建相关文章的发布短代码

把下面的代码写入到你的主题文件夹中的function.php中


点击(此处)折叠或打开

  1. function related_posts_shortcode( $atts ) {
  2.  extract(shortcode_atts(array(
  3.  'limit' => '5',
  4.  ), $atts));
  5.  
  6.  global $wpdb, $post, $table_prefix;
  7.  
  8.  if ($post->ID) {
  9.  $retval = '
      ';
    •  // Get tags
    •  $tags = wp_get_post_tags($post->ID);
    •  $tagsarray = array();
    •  foreach ($tags as $tag) {
    •  $tagsarray[] = $tag->term_id;
    •  }
    •  $tagslist = implode(',', $tagsarray);
    •  
    •  // Do the query
    •  $q = "SELECT p.*, count(tr.object_id) as count
    •  FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr, $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
    •  AND p.post_status = 'publish'
    •  AND p.post_date_gmt < NOW()
    •  GROUP BY tr.object_id
    •  ORDER BY count DESC, p.post_date_gmt DESC
    •  LIMIT $limit;";
    •  
    •  $related = $wpdb->get_results($q);
    •  if ( $related ) {
    •  foreach($related as $r) {
    •  $retval .= '
    •  
    • .wptexturize($r->post_title).'" href="'.get_permalink($r->ID).'">'.wptexturize($r->post_title).'

    • ';
    •  }
    •  } else {
    •  $retval .= '
    •  
    • No related posts found

    • ';
    •  }
    •  $retval .= '

  10. ';
  11.  return $retval;
  12.  }
  13.  return;
  14. }
  15. add_shortcode('related_posts', 'related_posts_shortcode');

以后,你就可以在博客的任意位置插入下面的短代码,来实现相关文章的调用显示了。

[AD:UV胶水去除气泡的方法:]

[related_posts]
3,同一分类下的相关文章

实现了相关文章的调用后,有的人又希望列出的文章和原文是同一分类下的,那么如何实现呢?

首先,同样把下面的代码写入的主题文件夹中的function.php中


点击(此处)折叠或打开

  1. /**
  2.  * related post with category
  3.  * @param: int $limit limit of posts
  4.  * @param: bool $catName echo category name
  5.  * @param: string $title string before all entries
  6.  * Example: echo fb_cat_related_posts();
  7.  */
  8. if ( !function_exists('fb_get_cat_related_posts') ) {
  9.  function fb_get_cat_related_posts( $limit = 5, $catName = TRUE, $title = '

    Recent Pages

    '
    ) {
  10.  
  11.  if ( !is_single() )
  12.  return;
  13.  
  14.  $limit = (int) $limit;
  15.  $output = '';
  16.  $output .= $title;
  17.  
  18.  $category = get_the_category();
  19.  $category = (int) $category[0]->cat_ID;
  20.  
  21.  if ( $catName )
  22.  $output .= __( 'Kategorie: ' ) . get_cat_name($category) . ' ';
  23.  
  24.  $output .= '';
  25.  
  26.  return $output;
  27.  }
  28. }

之后,调用自定义的函数fb_get_cat_related_posts就可以了,该函数包括3个参数

  • $limit (int) 定义显示文章数,int型数据;
  • $catName (bool) 输出分类名称,bool型数据( TRUE or FALSE)
  • $title (string) String for a text before all entries
阅读(2282) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~