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

Like (0)
zhaolou的头像zhaolou
Previous 2025年3月19日
Next 2025年3月22日

相关推荐

  • wordpress调用指定分类目录文章的方法

    不过对于没有代码开发的朋友来说,在用wordpress网站制作时可能会遇到一些问题,比如调用指定分类目录下的文章,今天就教大家一个方法怎样通过代码来调用wordpress指定分类目录下的文章,以便wordpress用户能更快上手使用wordpress。 我们在wordpress 主题开发时不管是哪个页面还是分类页,可以进行如下操作 <?php $pos…

    外贸 2025年3月22日
    83000
  • 如何做好页面标题标签内容优化编辑-Title Tags SEO优化全面指南(内附谷歌官方标题优化文档)

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

    google seo 2025年4月23日
    1.4K00
  • 如何用 Semrush + Google Trends + Ahrefs 高效挖掘关键词?

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

    google seo 2025年5月14日
    1.6K00
  • 常用的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日
    31100
  • RankMath SEO插件导致TAG页面无法收录的解决办法

    1、GSC报错 2、看了受影响的页面,果然! 3、再去看RankMathSEO插件生成的sitemap,有post,page,category,但是也没有tag,那就是插件本身并没有开启收录tag页面。 RankMath SEO插件解决tag页面正常收录的解决办法1、RankMath SEO-post-tag-tag archives robots meta…

    2025年4月13日
    85900

发表回复

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

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

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

关注微信