我是靠谱客的博主 多情微笑,最近开发中收集的这篇文章主要介绍wordpress提取文章(最新,最热,随机),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. WordPress中获取最新文章的代码

Php代码   收藏代码
  1. <li>  
  2.     <h2>最新文章</h2>  
  3.     <?php query_posts('showposts=6&cat=-111'); ?>  
  4.     <ul>  
  5.         <?php while (have_posts()) : the_post(); ?>  
  6.         <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
  7.         <?php endwhile;?>  
  8.      </ul>  
  9. </li>  
 

上面代码按照行号解释如下:

  • 小模块的标题
  • 读取6篇文章,排除分类ID为111里面的文章
  • 文章列表开始
  • 循环读取每一条文章
  • 显示文章标题、链接
  • 循环结束
  • 文章列表结束
  • 小模块结束

关于上面代码中 query_posts() 函数的用法, 可以看这里:

说说WordPress的主查询函数-query_posts()

 

来源: http://www.zuluo.net/2012/2012-04/wordpress-recent-post-code.html

 

2. WordPress显示最新文章,最热文章,随机显示文章代码

 

WordPress很多插件可以实现相关文章的功能,插件的优点是配置简单,但是可 能会对网站的速度造成一些小的影响,所以很多人还是比较喜欢用代码实现需要的功能,但是话又说回来了,代码实现也有缺点,就是配置复杂,不懂代码的人完全 摸不着头脑或者只能照搬别人的代码,还不如用插件。

这里我整理编写了几种用代码实现相关文章的方法,这其中会详细标明各部分代码的作用,以及如何自定义你想要的功 能,希望对大家有所帮助,有什么问题可以给本文发表评论,我会及时给你回复。开始之前,说明一点,以下所有方法输出的HTML代码格式都是以下形式,你可 以根据需要进行修改:
Html代码   收藏代码
  1. <ul id="xxx">  
  2. <li><a title="文章标题1" rel="bookmark" href="文章链接1">文章标题1</a></li>  
  3. <li><a title="文章标题2" rel="bookmark" href="文章链接2">文章标题2</a></li>  
  4. ......  
  5. </ul>  
 
方法一:标签相关

首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:

Php代码   收藏代码
  1. <ul id="tags_related">  
  2. <?php  
  3. $post_tags = wp_get_post_tags($post->ID);  
  4. if ($post_tags) {  
  5.   
  6. foreach ($post_tags as $tag)  
  7. {  
  8. // 获取标签列表  
  9. $tag_list[] .= $tag->term_id;  
  10. }  
  11.   
  12. // 随机获取标签列表中的一个标签  
  13. $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];  
  14.   
  15. // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表  
  16. $args = array(  
  17. 'tag__in' => array($post_tag),  
  18. 'category__not_in' => array(NULL), // 不包括的分类ID  
  19. 'post__not_in' => array($post->ID),  
  20. 'showposts' => 6, // 显示相关文章数量  
  21. 'caller_get_posts' => 1  
  22. );  
  23. query_posts($args);  
  24.   
  25. if (have_posts()) :  
  26. while (have_posts()) : the_post(); update_post_caches($posts); ?>  
  27. <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>  
  28. <?php endwhileelse : ?>  
  29. <li>* 暂无相关文章</li>  
  30. <?php endif; wp_reset_query(); } ?>  
  31. </ul>  
 

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

 

方法二:分类相关

本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。

Php代码   收藏代码
  1. <ul id="cat_related">  
  2. <?php  
  3. $cats = wp_get_post_categories($post->ID);  
  4. if ($cats) {  
  5. $args = array(  
  6. 'category__in' => array$cats[0] ),  
  7. 'post__not_in' => array$post->ID ),  
  8. 'showposts' => 6,  
  9. 'caller_get_posts' => 1  
  10. );  
  11. query_posts($args);  
  12.   
  13. if (have_posts()) :  
  14. while (have_posts()) : the_post(); update_post_caches($posts); ?>  
  15. <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>  
  16. <?php endwhileelse : ?>  
  17. <li>* 暂无相关文章</li>  
  18. <?php endif; wp_reset_query(); } ?>  
  19. </ul>  
 
方法三:标签相关,SQL获取

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

Php代码   收藏代码
  1. <ul id="tags_related">  
  2. <?php  
  3. $post_tags = wp_get_post_tags($post->ID);  
  4. if ($post_tags) {  
  5. $tag_list = '';  
  6. foreach ($post_tags as $tag)  
  7. {  
  8. // 获取标签列表  
  9. $tag_list .= $tag->term_id.',';  
  10. }  
  11. $tag_list = substr($tag_list, 0, strlen($tag_list)-1);  
  12.   
  13. $related_posts = $wpdb->get_results("  
  14. SELECT post_title, ID  
  15. FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy  
  16. WHERE {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id  
  17. AND ID = object_id  
  18. AND taxonomy = 'post_tag'  
  19. AND post_status = 'publish'  
  20. AND post_type = 'post'  
  21. AND term_id IN (" . $tag_list . ")  
  22. AND ID != '" . $post->ID . "'  
  23. ORDER BY RAND()  
  24. LIMIT 6");  
  25. // 以上代码中的 6 为限制只获取6篇相关文章  
  26. // 通过修改数字 6,可修改你想要的文章数量  
  27.   
  28. if ( $related_posts ) {  
  29. foreach ($related_posts as $related_post) {  
  30. ?>  
  31. <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>  
  32. <?php } } else { ?>  
  33. <li>暂无相关文章</li>  
  34. <?php } } ?>  
  35. </ul>  
  

方法四:分类相关,SQL获取

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

Php代码   收藏代码
  1. <ul id="cat_related">  
  2. <?php  
  3. $cats = wp_get_post_categories($post->ID);  
  4. if ($cats) {  
  5.   
  6. $related = $wpdb->get_results("  
  7. SELECT post_title, ID  
  8. FROM {$wpdb->prefix}posts, {$wpdb->prefix}term_relationships, {$wpdb->prefix}term_taxonomy  
  9. WHERE {$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id  
  10. AND {$wpdb->prefix}term_taxonomy.taxonomy = 'category'  
  11. AND {$wpdb->prefix}term_taxonomy.term_taxonomy_id = {$wpdb->prefix}term_relationships.term_taxonomy_id  
  12. AND {$wpdb->prefix}posts.post_status = 'publish'  
  13. AND {$wpdb->prefix}posts.post_type = 'post'  
  14. AND {$wpdb->prefix}term_taxonomy.term_id = '" . $cats[0] . "'  
  15. AND {$wpdb->prefix}posts.ID != '" . $post->ID . "'  
  16. ORDER BY RAND( )  
  17. LIMIT 6");  
  18.   
  19. if ( $related ) {  
  20. foreach ($related as $related_post) {  
  21. ?>  
  22. <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>  
  23. <?php } } else { ?>  
  24. <li>* 暂无相关文章</li>  
  25. <?php } }?>  
  26. </ul>  
  

方法五:作者相关

该方法是获取该文章作者的其他文章来充当相关文章,代码如下:

Php代码   收藏代码
  1. <ul id="author_related">  
  2. <?php  
  3. $post_author = get_the_author_meta( 'user_login' );  
  4. $args = array(  
  5. 'author_name' => $post_author,  
  6. 'post__not_in' => array($post->ID),  
  7. 'showposts' => 6, // 显示相关文章数量  
  8. 'orderby' => date// 按时间排序  
  9. 'caller_get_posts' => 1  
  10. );  
  11. query_posts($args);  
  12.   
  13. if (have_posts()) :  
  14. while (have_posts()) : the_post(); update_post_caches($posts); ?>  
  15. <li>* <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>  
  16. <?php endwhileelse : ?>  
  17. <li>* 暂无相关文章</li>  
  18. <?php endif; wp_reset_query(); ?>  
  19. </ul>  
  

时间效率对比

我们将用之前的一个php代码对以上各个相关文章代码执行时间进行测算,以便对以上各个的方法进行效率,给你的选择提供参考。以下是在同一篇文章中获取6篇相关文章,以上各方法最终测算的时间如下:

方法一:0.18067908287048 秒

方法二:0.057158946990967 秒

方法三:0.037126064300537 秒

方法四:0.045628070831299 秒

方法五:0.023991823196411 秒

 

来源: http://dongshao.net/573.html

 

3. wordpress如何获取某个分类下的文章?

 

只需通过get_posts来获取分类ID就可以输出分类下的文章,以及通过numberposts来控制文章显示的数量。

Java代码   收藏代码
  1. <?php $posts = get_posts( "category=4&numberposts=10" ); ?>  
  2. <?php if( $posts ) : ?>  
  3. <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>  
  4. <li>  
  5. <a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a>  
  6. </li>  
  7. <?php endforeach; ?>  
  8. </ul>  
  9. <?php endif; ?>  
 

来源: http://www.52shopex.com/wordpress_research/get_cat_article_84.html

 

4. How to List Your Most Popular Posts in WordPress

 

I decided my sidebar was looking a little plain. It needed something more than just ads, so I placed a list of my popular posts there. I think it looks pretty cool and it was very simple to do. I just created a PHP function and added it to my functions.php files and then added some styles to get it to look the way I wanted.

First lets create the function. We’ll call it popularPosts. Be sure to add it between tags.

Php代码   收藏代码
  1. function popularPosts($num) {  
  2.     global $wpdb;  
  3.       
  4.     $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");  
  5.       
  6.     foreach ($posts as $post) {  
  7.         setup_postdata($post);  
  8.         $id = $post->ID;  
  9.         $title = $post->post_title;  
  10.         $count = $post->comment_count;  
  11.           
  12.         if ($count != 0) {  
  13.             $popular .= '<li>';  
  14.             $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';  
  15.             $popular .= '</li>';  
  16.         }  
  17.     }  
  18.     return $popular;  
  19. }  
 

Now to call the function, all we need to do is place this code wherever we want our list to appear. Let’s call for a list of 10 items by placing a ’10′ as our $num variable when we call our function.

Php代码   收藏代码
  1. <div class="popular">  
  2.     <h2>Most Popular Posts</h2>  
  3.     <ul>  
  4.         <?php echo popularPosts(10); ?>  
  5.     </ul>  
  6. </div>  
 

来源: http://bavotasan.com/2009/how-to-list-your-most-popular-posts-in-wordpress/

最后

以上就是多情微笑为你收集整理的wordpress提取文章(最新,最热,随机)的全部内容,希望文章能够帮你解决wordpress提取文章(最新,最热,随机)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部