11 次查询 耗时 0.079 秒
本文共计687个字,1条留言,预计阅读时长3分钟
登录/注册
  • 首页
  • >
  • Notes
  • >
  • WordPress调用置顶文章
  • WordPress调用置顶文章

    作者:Misshylsay

    日期:2020年09月28日

    需要调用置顶文章时,代码如下:

    <?php query_posts(array("category__in"=>array(get_query_var("cat")), "post__in" => get_option("sticky_posts")));while(have_posts()) : the_post(); ?>
    <!--置顶文章循环块-->
    <?php endwhile;wp_reset_query();?>
    <?php while(have_posts()) : the_post(); ?><?php if(!is_sticky()){?>
    <!--普通文章循环块-->
    <?php } endwhile;?>
    

    以上代码是用在分类页的,get_query_var("cat"))是获取当前分类页ID,"post__in" => get_option("sticky_posts")就是查询置顶文章,is_sticky()用于判断是否为置顶文章。

    输出全部的置顶文章,代码如下

    <?php 
        $sticky = get_option('sticky_posts'); 
        rsort( $sticky );  //对数组逆向排序,即大ID在前 
        $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();     
    ?> 
    <!--置顶文章循环块-->
    <?php endwhile; endif; ?> 
    

     

    “WordPress调用置顶文章”共有1条评论

    1. 此方法可以完美解决置顶文章与普通文章同页面显示的问题。

      河南省漯河市 联通

    发表评论回复给 Misshylsay 点击这里取消回复。

    B em del U Link Code Quote