5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9829|回复: 0
打印 上一主题 下一主题

[标签] WordPress调用最新,随机,热门,指定分类代码汇总

[复制链接]

670

主题

785

帖子

8273

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8273
跳转到指定楼层
楼主
发表于 2021-5-29 00:10:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
关于WP文章调用方法,包括调用最新,指定分类,随机,热文等代码,经测试,支持最新版Wordpress。

1调用最新文章
  1. <?php query_posts('showposts=6&cat=-111'); ?>  // 显示篇数和排除分类

  2. <ul>  

  3. <?php while (have_posts()) : the_post(); ?>  

  4. <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a</li>  

  5. <?php endwhile;?>  

  6. </ul>  
复制代码

2调用指定分类文章
  1. <ul>

  2. <?php

  3. $args=array(

  4. 'cat' => 1,   // 分类ID

  5. 'posts_per_page' => 10, // 显示篇数

  6. );

  7. query_posts($args);

  8. if(have_posts()) : while (have_posts()) : the_post();

  9. ?>

  10. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

  11. <?php  endwhile; endif; wp_reset_query(); ?>

  12. </ul>
复制代码

3调用整站随机文章
  1. <ul>

  2. <?php

  3. $args = array( 'numberposts' => 5, 'orderby' => 'rand', 'post_status' => 'publish' );

  4. $rand_posts = get_posts( $args );

  5. foreach( $rand_posts as $post ) : ?>

  6. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

  7. <?php endforeach; ?>

  8. </ul>
复制代码

4调用同分类随机文章
  1. <ul>

  2. <?php

  3. $cat = get_the_category();

  4. foreach($cat as $key=>$category){

  5. $catid = $category->term_id;}

  6. $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); // 显示篇数

  7. $query_posts = new WP_Query();

  8. $query_posts->query($args);

  9. while ($query_posts->have_posts()) : $query_posts->the_post();?>

  10. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

  11. <?php endwhile;?>

  12. <?php wp_reset_query(); ?>

  13. </ul>
复制代码

5调用整站热门文章(按评论数)
  1. <ul>

  2. <?php

  3. $post_num = 10; // 显示篇数

  4. $args = array(

  5. ‘post_password’ => ”,

  6. ‘post_status’ => ‘publish’, // 只选公开的文章.

  7. ‘post__not_in’ => array($post->ID),//排除当前文章

  8. ‘caller_get_posts’ => 1, // 排除置顶文章.

  9. ‘orderby’ => ‘comment_count’, // 依评论数排序.

  10. ‘posts_per_page’ => $post_num

  11. );

  12. $query_posts = new WP_Query();

  13. $query_posts->query($args);

  14. while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>

  15. <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

  16. <?php } wp_reset_query();?>

  17. </ul>
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2024-6-16 13:47 , Processed in 0.046875 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表