WP网站分类列表不显示置顶文章,内容丰富栏目较多的网站会对推荐的文章进行置顶显示,以便访客打开该分类时第一时间能看到管理员推荐的内容,如果分类列表显示网站所有的置顶文章,显然影响用户体验,因此只显示该分类的置顶推荐文章会更加友好。
在当前主题的分类列表模板中添加代码:
<?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(); ?>
在正常的分类列表中排除已经设置为置顶的文章:
<?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())来判断,表示如果不是置顶文章则显示。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END