wordpress调用某分类下的置顶文章

做一个基于 wordpress 的主题模板,需要调用到某一分类下的置顶文章,网上已经有太多的调用全局置顶文章的教程了,今儿就单纯分享一下调用指定分类下的置顶文章!

先上代码:

<ul>
    <?php $sticky = get_option('sticky_posts');
        rsort( $sticky );
        $sticky = array_slice( $sticky, 0, 50);//array_slice() 函数在数组中根据条件取出一段值,并返回。
        query_posts( 'p=6', 'cat' => 2, 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) );
        if (have_posts()) :
            while (have_posts()) : the_post(); ?>
                <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
            <?php endwhile;
        endif;
    ?>
</ul>

接着分析:

get_option('sticky_posts') 能调出数据库表 wp_options 下字段 option_name 为 sticky_posts 中保存的所有的置顶文章信息;

再使用 query_posts() 并指定分类 cat 及需要调用的文章数,来输出置顶文章信息,query_posts() 传送门

没有置顶文章时调用分类下最新文章!