首页 视频课程 WordPress开发文档文章循环输出与重置 置顶文章

置顶文章

2020-04-03 / 3337阅

本视频需要登录以后才能播放

登录

开发中,我们需要的是只显示置顶文章,给他们单独的样式。下面的文章就需要排除置顶文章。

先看只输出置顶的文章,直接写代码:

$args = array(
       'post_type' =>'post',
       'ignore_sticky_posts'=>1 //置顶的文章不优先,但是会显示
); 

下面的代码可以排除置顶的文章

$top = get_option( 'sticky_posts' );//用于获取置顶的文章 
<?php
       $args = array(
              'post_type' =>'post',
              'post__in'=>get_option( 'sticky_posts' ) //只输出置项的文章
              //'post__not_in'=>get_option( 'sticky_posts' ) //排除置项的文章 
       );
       $query = new WP_Query( $args );
       $i=1;
       while ($query->have_posts()):$query->the_post();
              ?>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <br />

       <?php
       $i++;
       endwhile;
?> 

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录