用wp建站的伙伴都知道文章可以置顶,然后前端可以调用到顶部显示,那么在主题开发中如何调用呢,今天就来记录一下,调用置顶文章和排除置顶文章的方法。

  wp/wordpress调用置顶文章及排除置顶文章,超详细!-QUI-Notes

1.调用5条置顶文章

<?php
            $sticky = get_option('sticky_posts');
            rsort( $sticky );
            $sticky = array_slice( $sticky, 0, 5); // 调用5条
            query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
            if (have_posts()) :while (have_posts()) : the_post();
 ?> 
            <li><a href="<?php the_permalink(); ?>" target="_blank"><?php the_title(); ?></a></li>
<?php  endwhile;
wp_reset_query(); // 必填
endif; 
?> 

2.排除置顶文章 

<?php  $the_query = new WP_Query( array( 'post__not_in' => get_option( 'sticky_posts' ) ) );
         if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();?>
            <div class="post"  id="post-<?php the_ID(); ?>">
                <p> <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
                <?php the_post_thumbnail('thumbnail'); ?> 
           </div>
<?php endwhile; ?>
<?php endif; ?>