Wordpress收集系列-相关文章代码

有个想法,就是做一个wordpress的收集,把平常模板用到的代码汇总下,以后可以随时查阅。另外,也可能我本身有个收集癖好了。第一弹是wordpress相关文章的代码,内容是比较多了。

1.标签相关

<ul id="tags_related">
<?php
global $post;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
  foreach ($post_tags as $tag) {
    // 获取标签列表
    $tag_list[] .= $tag->term_id;
  }

  // 随机获取标签列表中的一个标签
  $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];

  // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
  $args = array(
        'tag__in' => array($post_tag),
        'category__not_in' => array(NULL),  // 不包括的分类ID
        'post__not_in' => array($post->ID),
        'showposts' => 6,                           // 显示相关文章数量
        'caller_get_posts' => 1
    );
  query_posts($args);

  if (have_posts()) {
while (have_posts()) {
  the_post(); update_post_caches($posts); ?>
<li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

使用说明:"不包括的分类ID" 指的是相关文章不显示该分类下的文章,将同行的 NULL 改成文章分类的ID即可,多个ID就用半角逗号隔开。因为这里限制只显示6篇相关文章,所以不管给 query_posts() 的参数 tag_in 赋多少个值,都是只显示一个标签下的 6 篇文章,除非第一个标签有1篇,第二个标签有2篇,第三个有3篇。。。。。。所以如果这篇文章有多个标签,那么我们采取的做法是随机获取一个标签的id,赋值给 tag_in 这个参数,获取该标签下的6篇文章。

2.分类相关

<ul id="cat_related">
<?php
global $post;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
    $args = array(
          'category__in' => array( $cats[0] ),
          'post__not_in' => array( $post->ID ),
          'showposts' => 6,
          'caller_get_posts' => 1
      );
  query_posts($args);

  if (have_posts()) {
while (have_posts()) {
  the_post(); update_post_caches($posts); ?>
  <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  } 
  else {
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query(); 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

3.分类&&标签

这是来自willin的代码,经过zww的修改。选取文章的时候参考了标签和分类,标签不足,再取分类。

<h3>Related Posts</h3>
<ul>
<?php
$post_num = 5; // 数量设定.
$exclude_id = $post->ID; 
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
  $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
  $args = array(
          'post_status' => 'publish',
          'tag__in' => explode(',', $tags), // 只选 tags 的文章.
          'post__not_in' => explode(',', $exclude_id), // 排除出现过的文章.
          'caller_get_posts' => 1,
          'orderby' => 'comment_date', // 依评论日期排序.
          'posts_per_page' => $post_num
  );
  query_posts($args);
  while( have_posts() ) { the_post(); ?>
             <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
     <?php
          $exclude_id .= ',' . $post->ID; $i ++;
  } wp_reset_query();
}
if ( $i < $post_num ) { // 当tags的文章不足, 再取 category .
  $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  $args = array(
          'category__in' => explode(',', $cats), // 只选 category 的文章.
          'post__not_in' => explode(',', $exclude_id),
          'caller_get_posts' => 1,
          'orderby' => 'comment_date',
          'posts_per_page' => $post_num - $i
  );
  query_posts($args);
  while( have_posts() ) { the_post(); ?>
             <li><a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php $i++;
  } wp_reset_query();
}
if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
?>
</ul>

4.标签相关(sql方式)

获取相关文章的原理与方法一相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

<ul id="tags_related">
<?php
global $post, $wpdb;
$post_tags = wp_get_post_tags($post->ID);
if ($post_tags) {
    $tag_list = '';
    foreach ($post_tags as $tag) {
        // 获取标签列表
        $tag_list .= $tag->term_id.',';
    }
    $tag_list = substr($tag_list, 0, strlen($tag_list)-1);

    $related_posts = $wpdb->get_results("
        SELECT DISTINCT ID, post_title
        FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
        WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
        AND ID = object_id
        AND taxonomy = 'post_tag'
        AND post_status = 'publish'
        AND post_type = 'post'
        AND term_id IN (" . $tag_list . ")
        AND ID != '" . $post->ID . "'
        ORDER BY RAND()
        LIMIT 6");
        // 以上代码中的 6 为限制只获取6篇相关文章
        // 通过修改数字 6,可修改你想要的文章数量

    if ( $related_posts ) {
        foreach ($related_posts as $related_post) {
?>
    <li><a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php   } 
    }
    else {
      echo '<li>暂无相关文章</li>';
    } 
}
else {
  echo '<li>暂无相关文章</li>';
}
?>
</ul>

5.分类相关(sql方式)

获取相关文章的原理与方法二相似,不过在获取文章的时候是以SQL语句来直接读取数据库,从而随机获取6篇相关文章记录,而不是WordPress的函数query_posts().

<ul id="cat_related">
<?php
global $post, $wpdb;
$cats = wp_get_post_categories($post->ID);
if ($cats) {
  $related = $wpdb->get_results("
  SELECT post_title, ID
  FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy
  WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id
  AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'
  AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id
  AND {$wpdb->prefix}posts.post_status = 'publish'
  AND {$wpdb->prefix}posts.post_type = 'post'
  AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'
  AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'
  ORDER BY RAND( )
  LIMIT 6");

  if ( $related ) {
      foreach ($related as $related_post) {
?>
    <li>* <a href="<?php echo get_permalink($related_post->ID); ?>" rel="bookmark" title="<?php echo $related_post->post_title; ?>"><?php echo $related_post->post_title; ?></a></li>
<?php
    } 
  }
  else {
    echo '<li>* 暂无相关文章</li>';
  } 
}
else {
  echo '<li>* 暂无相关文章</li>';
}
?>
</ul>

6.作者相关

<ul id="author_related">
<?php
  global $post;
  $post_author = get_the_author_meta( 'user_login' );
  $args = array(
        'author_name' => $post_author,
        'post__not_in' => array($post->ID),
        'showposts' => 6,               // 显示相关文章数量
        'orderby' => date,          // 按时间排序
        'caller_get_posts' => 1
    );
  query_posts($args);

  if (have_posts()) {
    while (have_posts()) {
      the_post(); update_post_caches($posts); ?>
  <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
    }
  }
  else { 
    echo '<li>* 暂无相关文章</li>';
  }
  wp_reset_query();
?>
</ul>

0 条评论