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

相关推荐

  • 外贸网站内链SEO优化对搜索引擎排名影响及注意事项详解

    一. 什么是内部链接? 内部链接是同一域名下页面与页面之间的链接,客户通过点击锚文本链接进入到其他的页面,外部链接指的是其他域名指向本域名的链接。 二. 为什么内部链接对于SEO优化非常重要?   内部链接可以帮助谷歌查找,收录并理解你网站所有页面。 如果你有策略地布局内链,那么会让你的页面有效地传递权重(相当于提升页面权威度)…

    google seo 2025年4月23日
    2.1K00
  • 常用的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
  • 如何用 Semrush + Google Trends + Ahrefs 高效挖掘关键词?

    作为Google SEO中最核心的一步,关键词挖掘不仅关系到流量多少,还决定了你内容能否打入目标用户搜索视野。本文以“xxx”为例,带你实操一遍关键词挖掘与筛选的完整流程。 ✅ Step 1:用 Semrush 查询关键词基础数据(Keyword Overview) 打开 Semrush,在左侧点击【Keyword Overview】,输入你想查询的关键词,…

    google seo 2025年5月14日
    4.5K00
  • 如何做好页面标题标签内容优化编辑-Title Tags SEO优化全面指南(内附谷歌官方标题优化文档)

    想要提高网站页面的SEO优化,页面的标题标签优化是非常重要的一环,如果你会把页面的标题标题标签(Title Tag)优化做好,那么你的网站有显著的自然排名提升。 标题优化的好处如下: 1. 提升网站整体SEO质量; 2. 增加内容营销效果; 3. 提升搜索引擎排名; 4. 提高用户点击率CTR(Click-through Rate) A. 什么是标题标签? …

    google seo 2025年4月23日
    2.7K00
  • Kadence 与 Astra 对比分析(2025最新版)

    对比维度 Kadence Astra 🌐 官方网站 kadencewp.com wpastra.com 🚀 加载速度 极快(Gutenberg 原生优化) 极快(轻量 HTML 构建) 🎨 设计灵活性 Header/Footer Builder + 内置区块设计(Kadence Blocks) Header/Footer Builder + Elemento…

    外贸 2025年5月14日
    3.2K00

发表回复

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

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

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

关注微信