0

So on my blog page I'm first loading the 2 most recent articles and display them at the top of the page, then I'm getting the rest of the post beneath. This is the code:

 <?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; ?>

AS you can see I have enabled pagination. Now I only need the first two posts at the top to display on the first page of the pagination, not on any of the others.

I've tried it like this:

    <?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; ?>

But this doesn't work and breaks the site. Any ideas what I'm doing wrong?

1 Answer 1

0

At a quick glance, it looks like this is nothing to do with your use of is_paged() (which looks correct) but that your closing if and endforeach statements are the wrong way round? You need to close the foreach() before you close the if().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.