..start..fly..

扫一扫
Guestbook

Home / PHP & Wordpress / 非插件实现wordpress缩略图显示相关文章

<< wordpress添加底部或是任何位置的widget兔年在即,更换兔年flash了 >>

因为我的网站是杂志类的主题,所以难免图片比较多,所以想把我原来的文字格式显示的相关文章更换成缩略图形式的相关文章,网上找了一下没有几篇比较实用的介绍。其实,自己的这个主题就采用了大量的缩略图形式的文章显示,所以直接从主题中扒代码好了,而且简单的美化了一下,加了一段css:

先来个图片演示一下好了:

缩略图显示相关文章

具体实现过程如下:

首先当然是要实现相关文章功能,这方面有很多插件可以使用,代码实现也是可以的,而且网络上也流传了很多方法,大概有五种左右吧,我个人使用感觉还是代码好了,毕竟这个功能不是必须的,我觉得比较好的一段代码是以下的,直接放在single.php的合适位置即可:

  1. <h2>Related Posts</h2>
  2. <ul id=“tags_related”>
  3. <?php
  4. $post_tags = wp_get_post_tags($post->ID);
  5. if ($post_tags) {
  6. foreach ($post_tags as $tag)
  7. {
  8. $tag_list[] .= $tag->term_id;
  9. }
  10. $post_tag = $tag_list[ mt_rand(0, count($tag_list) – 1) ];
  11. $args = array(
  12. ‘tag__in’ => array($post_tag),
  13. ‘category__not_in’ => array(NULL),
  14. ‘post__not_in’ => array($post->ID),
  15. ‘showposts’ => 5,
  16. ‘caller_get_posts’ => 1
  17. );
  18. query_posts($args);
  19. if (have_posts()) :
  20. while (have_posts()) : the_post(); update_post_caches($posts); ?>
  21. <li><a href=“<?php the_permalink(); ?>” rel=“bookmark” title=“<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>
  22. <?php endwhile; else : ?>
  23. <li>暂无相关文章</li>
  24. <?php endif; wp_reset_query(); } ?>
  25. </ul>

实现了相关文章之后就是要在这段代码里把列表形式替换成为缩略图形式的了,需要把一下的这段代码做更改:

  1. <li><a href=“<?php the_permalink(); ?>” rel=“bookmark” title=“<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>

 

直接替换为以下这段代码:

  1. <li><a href=“<?php the_permalink() ?>” title=“<?php the_title() ?>”><img src=“<?php $key=”thumbnail“; echo get_post_meta($post->ID, $key, true); ?>” width =“110” height=“115” alt=“<?php the_title() ?>” /></a></li>
  2. <?php the_excerpt(); ?>

 

最终的实现代码是这段代码:

  1. <h2>Related Posts</h2>
  2. <ul id=“tags_related”>
  3. <?php
  4. $post_tags = wp_get_post_tags($post->ID);
  5. if ($post_tags) {
  6. foreach ($post_tags as $tag)
  7. {
  8. $tag_list[] .= $tag->term_id;
  9. }
  10. $post_tag$tag_list[ mt_rand(0, count($tag_list) – 1) ];
  11. $argsarray(
  12. ‘tag__in’ => array($post_tag),
  13. ‘category__not_in’ => array(NULL),
  14. ‘post__not_in’ => array($post->ID),
  15. ‘showposts’ => 5,
  16. ‘caller_get_posts’ => 1
  17. );
  18. query_posts($args);
  19. if (have_posts()) :
  20. while (have_posts()) : the_post(); update_post_caches($posts); ?>
  21. <li><a href=“<?php the_permalink() ?>” title=“<?php the_title() ?>”><img src=“<?php $key=”thumbnail“; echo get_post_meta($post->ID, $key, true); ?>” width =“110” height=“115” alt=“<?php the_title() ?>” /></a></li>
  22. <?php the_excerpt(); ?>
  23. </div>
  24. <?php endwhileelse : ?>
  25. <li>暂无相关文章</li>
  26. <?php endif; wp_reset_query(); } ?>
  27. </ul>

 


PS:在通过这种方式实现的缩略图相关文章功能中,有两点要注意一下:

1.如果是特别古老的主题就需要在function.php中开启缩略图功能,现在大家用的主题一般都是不需要的,所以可以跳过了;

2.在发布文章的时候在“添加自定义栏目”里添加thumbnail,在其右边的值中输入图片地址就可以了的。

这样添加完了以后其实觉得还是不够美观的,可以简单的用CSS来控制一下这个缩略图文章的整体大小,使和主题页面更加协调,我的CSS非常简单,因为主题颜色比较简单,把这段CSS添加到使用的主题的根目录下的style.css的尾部就可以了的,可以自行修改大小、边距、颜色,我的CSS如下:

  1. .entry_b{
  2. width:629px;
  3. border: 1px solid #e5e5e5;
  4. }
  5. .relatedposts {
  6. float: left;
  7. width:110px;
  8. padding: 7px;
  9. }

 

然后通过这段CSS限制相关文章缩略图的现实格式,我的single.php中最终的相关文章代码如下:

  1. <h2>Related Posts</h2>
  2. <div class=“entry_b”>
  3. <ul id=“tags_related”>
  4. <?php
  5. $post_tags = wp_get_post_tags($post->ID);
  6. if ($post_tags) {
  7. foreach ($post_tags as $tag)
  8. {
  9. $tag_list[] .= $tag->term_id;
  10. }
  11. $post_tag = $tag_list[ mt_rand(0, count($tag_list) – 1) ];
  12. $args = array(
  13. ‘tag__in’ => array($post_tag),
  14. ‘category__not_in’ => array(NULL),
  15. ‘post__not_in’ => array($post->ID),
  16. ‘showposts’ => 5,
  17. ‘caller_get_posts’ => 1
  18. );
  19. query_posts($args);
  20. if (have_posts()) :
  21. while (have_posts()) : the_post(); update_post_caches($posts); ?>
  22. <div class=“relatedposts”>
  23. <a href=“<?php the_permalink() ?>” title=“<?php the_title() ?>”><img src=“<?php $key=”thumbnail“; echo get_post_meta($post->ID, $key, true); ?>” width =“110” height=“115” alt=“<?php the_title() ?>” /></a>            <h3><a href=“<?php the_permalink() ?>” title=“<?php the_title() ?>”><?php the_title() ?></a></h3>
  24. <?php the_excerpt(); ?>
  25. </div>
  26. <?php endwhile; else : ?>
  27. <li>暂无相关文章</li>
  28. <?php endif; wp_reset_query(); } ?>
  29. </ul>
  30. <div class=“clear”></div>
  31. </div>
  32. <!– end: relatedposts –>

 

当然如果你不需要简单美化一下,用上面的那段没有CSS美化的也是可以的,相信大家一定做的更好,兔年新年快乐,祝wordpress更加强大,大家都更加强大 :lol:

Related Posts

转载原创文章请注明,转载自:SKY..fly..[www.sky.gs]

本文链接: https://www.sky.gs/websites/phpwordpress/nonplugin-wordpress-thumbnail-related-post.html

QR:  非插件实现wordpress缩略图显示相关文章

9 Responses to “非插件实现wordpress缩略图显示相关文章”

  1. 流金漩涡 says:

    可以把这功能加入到rss里吗

  2. 亦斯 says:

    多谢,已搞定!

Leave a Reply

Code (☆)2+1=?


Websites powered by Wordpress6.7.1 Copyright © 2009-2025 - All Rights Reserved   SKY..fly..