因此,在我的博客页面上,我首先加载最近的两篇文章,并将它们显示在页面顶部,然后我将文章的其余部分放在下面。代码如下:
<?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; ?>
但这不起作用,破坏了网站。知道我做错了什么吗?