仅在分页的第一页加载某些内容

时间:2017-06-07 作者:user52738

因此,在我的博客页面上,我首先加载最近的两篇文章,并将它们显示在页面顶部,然后我将文章的其余部分放在下面。代码如下:

 <?php 
$args = array( \'numberposts\' => 2, \'post_status\'=>"publish",\'post_type\'=>"post",\'orderby\'=>"post_date", \'cat\'=>\'-8, -9, -7, -6, -5, -4\', \'paged\'=> $paged);
$postslist = get_posts( $args );

echo \'<div class="latest_new_posts">\';
    echo\'<div class="col-lg-1 col-md-1"></div>\';
 foreach ($postslist as $post) :  setup_postdata($post); ?> 
 <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12 mobile-padding" >
     <div class="blog-date-news"><span><?php the_date(\'d/m/Y\'); ?></span></div>
     <div class="blog-container">
     <div class="news-blog-title"><span><?php the_title(); ?></span></div>
     <div class="news-blog-excerpt"> <?php echo excerpt(700); ?> </div>
     <div class="news-blog-more"><a href="<?php the_permalink(); ?>"> <img src="..."/></a></div>
</div>
    </div>

<?php endforeach; ?>
<div class="col-lg-1 col-md-1"></div>
</div>
</div>
<div class="container white" style="padding:50px 0;">
    <div class="container-max">
       <?php $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : \'1\'; ?>

        <?php 
$args = array( \'numberposts\' => 5,  \'post_status\'=>"publish",\'post_type\'=>"post",\'orderby\'=>"post_date",\'cat\'=>\'-8, -9, -7, -6, -5, -4\',\'paged\'=> $paged);
$postslist = get_posts( $args );
echo \'<div class="latest_new_posts main-news">\';
?>

 <?php 
     $wp_query = new WP_Query(\'$args\'.$paged);
    ?>

    <?php if ( $wp_query->have_posts() ) : ?>
    <?php while ( $wp_query->have_posts() ) : the_post(); ?>   
<?php foreach ($postslist as $post) :  setup_postdata($post); ?> 
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="position:relative; margin:45px 0;" >
     <div class="blog-date-news main-news-date"><span><?php the_date(\'d/m/Y\'); ?></span></div>
     <div class="blog-container main-news-container">
     <div class="news-blog-title"><span><?php the_title(); ?></span></div>
     <div class="news-blog-excerpt"> <?php echo excerpt(500); ?> </div>
     <div class="news-blog-more main-news-blog-more"><a href="<?php the_permalink(); ?>"> <img src="..."/></a></div>
</div>
    </div>
<?php endforeach; ?>

    <?php endwhile; ?>

 <div class="blog-page-next"><?php next_posts_link( \'Next\', $wp_query->max_num_pages );
             ?> </div>

     <?php wp_reset_postdata(); ?>
    <?php else : ?>
    <p><?php __(\'No News\'); ?></p>
    <?php endif; ?>
如您所见,我已启用分页。现在,我只需要顶部的前两篇文章就可以显示在分页的第一页上,而不需要显示在其他任何一页上。

我试过这样做:

    <?php 
$args = array( \'numberposts\' => 2, \'post_status\'=>"publish",\'post_type\'=>"post",\'orderby\'=>"post_date", \'cat\'=>\'-8, -9, -7, -6, -5, -4\');
$postslist = get_posts( $args );
if( !is_paged() ) { 
echo \'<div class="latest_new_posts">\';
    echo\'<div class="col-lg-1 col-md-1"></div>\';
 foreach ($postslist as $post) :  setup_postdata($post); ?> 
 <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12 mobile-padding" >
     <div class="blog-date-news"><span><?php the_date(\'d/m/Y\'); ?></span></div>
     <div class="blog-container">
     <div class="news-blog-title"><span><?php the_title(); ?></span></div>
     <div class="news-blog-excerpt"> <?php echo excerpt(700); ?> </div>
     <div class="news-blog-more"><a href="<?php the_permalink(); ?>"> <img src="...."/></a></div>
</div>
    </div>
    };
<?php endforeach; ?>
但这不起作用,破坏了网站。知道我做错了什么吗?

1 个回复
SO网友:onebc

快速看一眼,这似乎与您使用is\\u paged()无关(看起来很正确),但您的结束if和endforeach语句的方式是错误的?在关闭if()之前,需要先关闭foreach()。

结束

相关推荐

Pagination custom query

我正在尝试制作一个自定义页面模板,以显示浏览次数最多的帖子。我可以回复帖子,但我很难弄清楚如何分页。以下是我所拥有的:$args = array(\'orderby\' => \'meta_value_num\', \'meta_key\' => \'post_views_count\', \'posts_per_page\' => 36 ); $loop = new WP_Query( $args ); while ( $