wordpress分类列表调用该分类置顶文章
网站文章 2026-09-21 07:53:25 4 0

WP网站分类列表不显示置顶文章,内容丰富栏目较多的网站会对推荐的文章进行置顶显示,以便访客打开该分类时第一时间能看到管理员推荐的内容,如果分类列表显示网站所有的置顶文章,显然影响用户体验,因此只显示该分类的置顶推荐文章会更加友好。

在当前主题的分类列表模板中添加代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php 
    query_posts(array(
        "category__in" => array(get_query_var("cat")), 
        "post__in" => get_option("sticky_posts")
        )
    );
    while(have_posts()) : the_post(); 
?>
<h2>【置顶】<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php 
    endwhile;
    wp_reset_query();
?>

在正常的分类列表中排除已经设置为置顶的文章:

1
2
3
4
5
<?php while(have_posts()) : the_post(); ?>
    <?php if(!is_sticky()){?>
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
<?php } endwhile;?>

其实就是在主循环中添加if(!is_sticky())来判断,表示如果不是置顶文章则显示。

文章说明
本文标签:
©版权声明
本站提供的一切软件、教程和游戏辅助仅限用于娱乐和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络收集,版权争议与本站无关。如有侵权请邮件与我们联系处理。敬请谅解!6594404@qq.com