1

试图在我的客户主页上显示一个英雄区域显示最近的置顶帖子,但我似乎一直遇到障碍。代码末尾的错误使我在死亡时出现白屏。这是我的代码:

<?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()?>
        }
?>
4

2 回答 2

0

你可以试试这段代码

<?php
    $sticky = get_option( 'sticky_posts' );
    rsort( $sticky );
    $sticky = array_slice( $sticky, 0, 1 );

if (is_numeric($sticky&#91;0&#93;)) {
    /* Query sticky posts */
    query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
        while ( have_posts() ) : the_post();
        the_title('<h3>', '</h3>');
        if($post->post_excerpt) :
            the_excerpt();
        else:
            the_content();
        endif;
        endwhile; // End the loop. Whew. 

    wp_reset_query();
}
?>
于 2018-08-03T04:10:07.153 回答
0

在您的代码中,您忘记}为您的第二个添加关闭if statement -> if(have_posts())。此外,我认为您不需要使用rsortarray_slice获得第一个最新的置顶帖子。您可以尝试使用下面的编辑代码(未经测试但应该可以):这将显示第一个最新的置顶帖子。如果没有置顶帖子,则将显示第一个最新的非置顶帖子。

<?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();
            }
        }
?>
于 2018-08-03T04:10:49.843 回答