在WordPress中,我们有时候要调用置顶的文章,并且关键的是控制显示置顶的文章的数量,你或许试了网上的很多方法,总有不如意的地方,今天分享自己用的一套,个人觉得应该还不错的!
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );//对数组逆向排序,即大ID在前
$sticky = array_slice( $sticky, 0, 2);//输出置顶文章数,0是起始位置,2是输出总条数
query_posts( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
if (have_posts()) :while (have_posts()) : the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p class="info"><?php if (has_excerpt()) {
echo $description = get_the_excerpt(); //文章编辑中的摘要
}else {
echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 60,"……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要
} ?></p>
<?php endwhile; endif; ?>
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1,'showposts' => 4,'cat' =>5) )
showposts’ => 4,’ 为设置置顶数量的。
cat’ =>5 为文章ID号
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
