0

I inhertited a WordPress page with a long post list on one page that uses get_posts(). I need it to have pagination to not load all the posts (images, content) at once. Would I have to rewrite everything with WP_Query() or is there an easier way? I set the "posts_per_page" argument to 5 from originally 25 but now I can't see the other posts.

<?php
$args = array(
    "posts_per_page"   => 5,
    "paged"            => 1,
    'category_name'    => 'Publishing',
    "orderby"          => "post_date",
    "order"            => "DESC",
    "post_type"        => "post",
    "post_status"      => "publish"
);

$posts_array = get_posts($args); 




 foreach($posts_array as $post)
 {  $id=$post->ID; $image=get_the_post_thumbnail($id, 'full'); 
    if( class_exists('Dynamic_Featured_Image') ) {
        global $dynamic_featured_image;
    
        $featured_images = $dynamic_featured_image->get_featured_images($id );
        
     }

     echo "<div class='direkt'>".get_the_date( 'd F Y', $post->ID )."<a href=".get_permalink($post->ID)."?kat=publishing> [Direct Link] </a></div>";
    ?>
        <div class="custompost_wrapper">
            <div class="custompost">
                <div class="splide" data-splide='{"lazyLoad": "nearby"}'>
                    <div class="splide__track">
                        <ul class="splide__list">
                            <li class="splide__slide">
                                <?php echo $image; ?>
                            </li>
                            <?php foreach($featured_images as $featured_image): ?>
                                <li class="splide__slide">
                                    <img src="<?php echo $featured_image['full']; ?>"
                                    data-splide-lazy="<?php echo $featured_image['full']; ?>"
                                    loading="lazy">
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

<?php

   echo "<div class='custompost_contentwrapper'>
        <strong>". $post->post_title . "</strong><br>";
   echo "<div class='thecontent'>" . $post->post_content . "</div>";
   echo "<div class='spacer'></div></div>";
 } 


?>
4
  • Do you mean the "featured images"? Those seem to be handled by a plugin; have you checked in with the plugin's support team?
    – Pat J
    Dec 2, 2021 at 15:47
  • It looks like you've already got a lazy loading image script? The data-splide-lazy bit.
    – Rup
    Dec 2, 2021 at 15:54
  • I mean that at the top I set "posts_per_page" => 5 from originally 25. But ideally it would not only load 5 but all of them (-1). But I don't want to load ALL of the posts at once but paginate or get a infinity loading. Basically how do I paginate this loop: foreach($posts_array as $post)
    – Lisa
    Dec 2, 2021 at 16:15
  • Using the built in pagination is only available when using WP_Query and not get_posts. Pagination when using wp_query? is my go to when ever I need to use pagination. Dec 13, 2021 at 9:09

0

Your Answer

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

Browse other questions tagged or ask your own question.