如何自定义代码为WordPress添加相关文章功能

如何为WordPress添加相关文章功能,并提供了标题列表样式和缩略图样式。相关文章的获取思路是:Tags标签相关>同分类下文章,也就是说,先获取标签相同的文章,如果还达不到数量,就调用该分类下的文章补足。获取方法貌似最初来自Willin Kan 大师,倡萌再次修改。

1.添加标题列表样式的相关文章

将下面的代码添加到 single.php 要显示相关文章的位置即可:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  1. <h3>相关文章</h3>
  2. <ul class="related_posts">
  3. <?php
  4. $post_num = 8;
  5. $exclude_id = $post->ID;
  6. $posttags = get_the_tags(); $i = 0;
  7. if ( $posttags ) {
  8. $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
  9. $args = array(
  10. 'post_status' => 'publish',
  11. 'tag__in' => explode(',', $tags),
  12. 'post__not_in' => explode(',', $exclude_id),
  13. 'caller_get_posts' => 1,
  14. 'orderby' => 'comment_date',
  15. 'posts_per_page' => $post_num,
  16. );
  17. query_posts($args);
  18. while( have_posts() ) { the_post(); ?>
  19. <li><a rel="bookmark" href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  20. <?php
  21. $exclude_id .= ',' . $post->ID; $i ++;
  22. } wp_reset_query();
  23. }
  24. if ( $i < $post_num ) {
  25. $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  26. $args = array(
  27. 'category__in' => explode(',', $cats),
  28. 'post__not_in' => explode(',', $exclude_id),
  29. 'caller_get_posts' => 1,
  30. 'orderby' => 'comment_date',
  31. 'posts_per_page' => $post_num - $i
  32. );
  33. query_posts($args);
  34. while( have_posts() ) { the_post(); ?>
  35. <li><a rel="bookmark" href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  36. <?php $i++;
  37. } wp_reset_query();
  38. }
  39. if ( $i == 0 ) echo '<li>没有相关文章!</li>';
  40. ?>
  41. </ul>

PS:第四行$post_num = 8;表示显示8篇文章,请根据自己的需要修改。

显示样式需要自己写css,可以参考一下下面的:

  1. 1
  2. 2
  1. .related_posts{margin-top:5px;}
  2. .related_posts li{margin-left:20px;color:#444;list-style:circle;font-size:14px;line-height:26px;padding:0 0 0 5px}

2.添加含缩略图的相关文章

倡萌只是根据上面的代码改了一下,添加了缩略图。

1)在主题的 functions.php 的最后一个 ?> 前添加下面的代码:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  1. //添加特色缩略图支持
  2. if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails');
  3.  
  4. //输出缩略图地址 From wpdaxue.com
  5. function post_thumbnail_src(){
  6. global $post;
  7. if( $values = get_post_custom_values("thumb") ) { //输出自定义域图片地址
  8. $values = get_post_custom_values("thumb");
  9. $post_thumbnail_src = $values [0];
  10. } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址
  11. $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
  12. $post_thumbnail_src = $thumbnail_src [0];
  13. } else {
  14. $post_thumbnail_src = '';
  15. ob_start();
  16. ob_end_clean();
  17. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  18. $post_thumbnail_src = $matches [1] [0]; //获取该图片 src
  19. if(empty($post_thumbnail_src)){ //如果日志中没有图片,则显示随机图片
  20. $random = mt_rand(1, 10);
  21. echo get_bloginfo('template_url');
  22. echo '/images/pic/'.$random.'.jpg';
  23. //如果日志中没有图片,则显示默认图片
  24. //echo '/images/default_thumb.jpg';
  25. }
  26. };
  27. echo $post_thumbnail_src;
  28. }

PS:上面的代码主要是获取图片链接,获取的顺序是:

自定义字段为 thumb 的图片>特色缩略图>文章第一张图片>随机图片/默认图片;

随机图片:请制作10张图片,放在现用主题文件夹下的 images/pic/ 目录,图片为jpg格式,并且使用数字 1-10命名,比如 1.jpg;如果你不想用随机图片,请将 倒数第5行 前面的“//”去掉,然后给 倒数第7、9行 前面添加“//”注销,并且在现用主题的 /images/ 目录下添加一张名字为 default_thumb.jpg 的默认图片,这样,就会显示默认图片。

2)将下面的代码添加到 single.php 要显示相关文章的位置:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  1. <h3>相关文章</h3>
  2. <ul class="related_img">
  3. <?php
  4. $post_num = 4;
  5. $exclude_id = $post->ID;
  6. $posttags = get_the_tags(); $i = 0;
  7. if ( $posttags ) {
  8. $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ',';
  9. $args = array(
  10. 'post_status' => 'publish',
  11. 'tag__in' => explode(',', $tags),
  12. 'post__not_in' => explode(',', $exclude_id),
  13. 'caller_get_posts' => 1,
  14. 'orderby' => 'comment_date',
  15. 'posts_per_page' => $post_num
  16. );
  17. query_posts($args);
  18. while( have_posts() ) { the_post(); ?>
  19. <li class="related_box" >
  20. <div class="r_pic">
  21. <a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank">
  22. <img src="<?php%20echo%20post_thumbnail_src();%20?>" alt="<?php the_title(); ?>" class="thumbnail" />
  23. </a>
  24. </div>
  25. <div class="r_title"><a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank" rel="bookmark"><?php the_title(); ?></a></div>
  26. </li>
  27. <?php
  28. $exclude_id .= ',' . $post->ID; $i ++;
  29. } wp_reset_query();
  30. }
  31. if ( $i < $post_num ) {
  32. $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
  33. $args = array(
  34. 'category__in' => explode(',', $cats),
  35. 'post__not_in' => explode(',', $exclude_id),
  36. 'caller_get_posts' => 1,
  37. 'orderby' => 'comment_date',
  38. 'posts_per_page' => $post_num - $i
  39. );
  40. query_posts($args);
  41. while( have_posts() ) { the_post(); ?>
  42. <li class="related_box" >
  43. <div class="r_pic">
  44. <a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank">
  45. <img src="<?php%20echo%20post_thumbnail_src();%20?>" alt="<?php the_title(); ?>" class="thumbnail" />
  46. </a>
  47. </div>
  48. <div class="r_title"><a href="<?php%20the_permalink();%20?>" title="<?php the_title(); ?>" target="_blank" rel="bookmark"><?php the_title(); ?></a></div>
  49. </li>
  50. <?php $i++;
  51. } wp_reset_query();
  52. }
  53. if ( $i == 0 ) echo '<div class="r_title">没有相关文章!</div>';
  54. ?>
  55. </ul>

PS:第四行$post_num = 4; 表示调用4篇文章,请根据自己需要修改。

css样式自己写,也可参考一下:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  1. .related_posts{margin-top:5px;}
  2. .related_img{width:600px;height:210px;}
  3. .related_box{float:left;overflow:hidden;margin-top:5px;width:148px;border-right:1px #eee solid}
  4. .related_box:hover{background:#f9f9f9}
  5. .related_box .r_title{width:auto;height:72px;font-weight:400;font-size:14px;margin:0 10px;overflow:hidden;}
  6. .related_box .r_pic{margin:6px}
  7. .related_box .r_pic img{width:130px;height:100px;border:1px solid #e1e1e1;background:#fff;padding:2px}
THE END