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>";
}
?>
WP_Query
and notget_posts
. Pagination when using wp_query? is my go to when ever I need to use pagination.