douyangcheng4965 2018-08-03 02:04
浏览 118
已采纳

仅限主页上的WordPress Sticky Post

Trying to get a hero area up on my clients home display the most recent sticky post, but I keep seeming to hit a snag. Error right at the end of the code which is making me get the white screen on death. Here's my code:

<?php if (is_home()) {
            $sticky = get_option( 'sticky_posts' ); // Get all sticky posts
            rsort( $sticky ); // Sort the stickies, latest first
            $sticky = array_slice( $sticky, 0, 1 ); // Number of stickies to show
            query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query
            if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
            <div class="trend-post">
            <div class="thumb"><?php the_post_thumbnail(); ?></div>
            <div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
            <?php endwhile;?>
            <?php wp_reset_query()?>
        }
?>
  • 写回答

2条回答 默认 最新

  • dps57553 2018-08-03 04:10
    关注

    In your code, you forgot to add closing } for your second if statement -> if(have_posts()). Also I don't think you will need to use rsort and array_slice to get the first latest sticky post. You may try to use the edited code below (not tested but should work): This will display first latest sticky post. If no sticky post then will display first latest non-sticky post.

    <?php if (is_home()) {
                $sticky = get_option( 'sticky_posts' ); // Get all sticky posts
    
                $args = array(
                      'posts_per_page' => 1,
                      'post__in'  => $sticky,
                      'ignore_sticky_posts' => 1
                );
                $query = new WP_Query( $args );
    
                if( $query->have_posts() ) {
                      while( $query->have_posts() ) {
                            $query->the_post();
                      ?>
                            <div class="trend-post">
                            <div class="thumb"><?php the_post_thumbnail(); ?></div>
                            <div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div></div>
                      <?php
                      }
                      wp_reset_query();
                }
            }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题