wordpress获取分类下文章列表四种方法

1、使用query_posts()函数
以下代码实际上使用query_posts()函数调取分类目录下的文章,showposts是调取的数量。

<?php
    $cats = get_categories();
    foreach ( $cats as $cat ) {
    query_posts( 'showposts=10&cat=' . $cat->cat_ID );
?>
    <h3><?php echo $cat->cat_name; ?></h3>
    <ul class="sitemap-list">
        <?php while ( have_posts() ) { the_post(); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php } wp_reset_query(); ?>
    </ul>
<?php } ?>

在官方文档中,这样强调:“如果我们不得不用到query_posts(),必须确保每次使用query_posts()后同时执行wp_reset_query();”。这就是为什么在上面的代码中加上了wp_reset_query()的原因。修改其中的数字10可以设定显示的篇数,可用于在单页面上显示全部分类文章。

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

<?php $posts = get_posts( "category=4&numberposts=10" ); ?>  
<?php if( $posts ) : ?>  
<ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>  
<li>  
<a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a>  
</li>  
<?php endforeach; ?>  
</ul>  
<?php endif; ?>

3、结合wp_list_categories()函数输出分类标题

<h2> <?php wp_list_categories('include=11&title_li=&style=none'); ?> </h2>
      <!--//输出 ID 为11的分类的标题 -->
         <?php  echo category_description(11); ?>
      <!--//输出 ID 为11的分类的描述 -->
          <?php query_posts('showposts=10&cat=11'); ?>
      <!-- //query_posts 给 The Loop 限定的条件是:显示12篇日志和分类 ID 为11 -->
           <?php while (have_posts()) : the_post(); ?>
      <!--//The Loop 开始 -->
     <li>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><? echo wp_trim_words( get_the_title(),24 ); ?></a>
       <?php  the_time('m/d'); ?>
     </li>
      <!-- //用列表的方式输出带有链接的文章标题-->
            <?php endwhile;wp_reset_query(); ?>
      <!--//The Loop 结束 -->

4、自定义函数

function popularPosts($num) {  
    global $wpdb;  
    $posts = $wpdb->get_results("SELECT comment_count, ID, post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , $num");    
    foreach ($posts as $post) {  
        setup_postdata($post);  
        $id = $post->ID;  
        $title = $post->post_title;  
        $count = $post->comment_count;  
          
        if ($count != 0) {  
            $popular .= '<li>';  
            $popular .= '<a href="' . get_permalink($id) . '" title="' . $title . '">' . $title . '</a> ';  
            $popular .= '</li>';  
        }  
    }  
    return $popular;  
}

主题测试文章,只做测试使用。发布者:zhaolou,转转请注明出处:https://www.zhaolou.cc/archives/4224

赞 (0)
zhaolou's avatarzhaolou
上一篇 2025年3月19日 下午7:19
下一篇 2025年3月22日 上午10:14

相关推荐

  • 美国苹果账号(Apple ID)注册方法

    准备工作: 1、提前注册一个国内的Apple ID(这个无需多说) 2、准备电脑一台,或者手机一部 开始注册美国Apple ID: 1、先进入苹果Apple ID官网https://appleid.apple.com/#!&page=signin,然后选择“登录”! 2、输入你提前准备好了的国内的Apple ID,输入账号和密码,并登录!(不建议使用…

    2025年6月1日
    2.9K00
  • wordpress开发建设常用调用代码

    1、获取页面分类列表 <?php query_posts(‘showposts=6&cat=54’); //cat=1为调用ID为1的分类下文章 while(have_posts()) : the_post(); ?> <li> <a href=”<?php echo the_permalink() ?>” …

    外贸 2025年3月22日
    1.9K00
  • WordPress在网站建设开发中进行随机文章调用输出的3个方法

    用wordpress系统进行网站开发时,经常会用到随机调用数据信息的需求。这样对于我们页面或是详细页中,这样每次访问时就会展示不同的数据,每次都会更新不同的数据,这样可以给网站用户访问一种每次不同的访问变化与数据展示。 1、最常见的调用随机代码,这样也比较容易懂,调用数据也比较简单。 <?php $args = array(&nbs…

    外贸 2025年3月22日
    1.8K00
  • 常用的Rank Math SEO 中英文对照

    Basic SEO Add Focus Keyword to the SEO title.Add Focus Keyword to your SEO Meta Description.Use Focus Keyword in the URL.Use Focus Keyword at the beginning of your content.Use Focu…

    google seo 2025年9月10日
    2.2K00
  • 2025 最新对比:Blocksy Pro、Kadence、Astra 功能+价格+性能全解析!

    总体结论: Blocksy Pro = 视觉表现最强 + 功能集最多 + 动效细腻Kadence = 轻便易用 + 电商友好 + 性能均衡Astra = 性能最优 + 生态强大 + 适合SEO和内容站 01. 功能模块对比功能模块Blocksy ProKadenceAstraHeader/Footer Builder✅ 拖拽+动态显示逻辑✅ 拖拽+条件逻辑支…

    外贸 2025年7月6日
    3.5K00

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

工作时间:周一至周五,9:00-18:00,节假日休息

关注微信