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

相关推荐

  • 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.3K00
  • Kadence 与 Astra 对比分析(2025最新版)

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

    外贸 2025年5月14日
    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日
    1.0K00
  • wordpress调用指定分类目录文章的方法

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

    外贸 2025年3月22日
    1.3K00
  • wordpress 进行文章列表分页

    把以下代码放入:functions.php function lingfeng_pagenavi( $range = 4 ) { global $paged,$wp_query; if ( !$max_page ) { $max_page = $wp_query->max_num_pages; } if( $max_page >1 ) { ech…

    外贸 2025年3月22日
    1.2K00

发表回复

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

联系我们

在线咨询: QQ交谈

邮件:156738051@qq.com

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

关注微信