I am creating a shortcode that outputs a custom loop. Within the loop while
, I am injecting certain content after a particular post count. This is done by checking the current_post
number. This particular function is quite long (and filled with commented-out code, from testing).
Here is a link to my current version (currently with ALL posts on a single page): https://gist.github.com/Garconis/c93ce89c3d378bfb7f5d8df97acbebc2
This works fine if/when ALL posts (posts_per_page => -1
) are showing, but as soon as I add pagination (e.g., set posts_per_page => 5
, the injected content is starting from a count of 0 on each new page.
How can I update this, to be able to inject my specific content after X posts, without the count starting over with each new page?
Bare bones of what I have:
<?php
// create shortcode to list all Featured Itineraries
add_shortcode( 'fs_burn_homepage_loop','fs_all_featured_itineraries_shortcode' );
function fs_all_featured_itineraries_shortcode( $atts ) {
ob_start();
// Query posts from the database.
$options = array(
// Arguments for your query.
'post_type' => array('post', 'ajde_events'),
'posts_per_page' => 5,
'paged' => 1,
);
/* To get the Easy Load More plugin to work */
if ( get_query_var( 'paged' ) ) {
$options['paged'] = get_query_var( 'paged' );
}
elseif ( get_query_var( 'page' ) ) {
$options['paged'] = get_query_var( 'page' );
} else {
$options['paged'] = 1;
}
$query = new WP_Query( $options );
// Check that we have query results
if ( $query->have_posts() ) {
// we have posts, so lets wrap them
echo'<div id="fs-homepage-loop" class="fs-all-posts">';
// Start looping over the query results.
while ( $query->have_posts() ) {
// Set up post data.
$query->the_post();
echo '<article id="post-'. get_the_ID() .'" class="fs-post-article '. join( ' ', get_post_class() ) .'">
<div class="fs-post-content-wrapper">
<div class="fs-post-name">
<h2 class="entry-title"><a href="'. get_permalink(); .'">'. get_the_title() .'</a></h2>
</div>
</div>
</article>';
// inject stuff after X posts
if( ($query->current_post == 3) ) {
echo '<article class="fs-post-article">
<div class="fs-post-content-wrapper">
content after post 4 is here
</div>
</article>';
}
if( ($query->current_post == 9) ) {
echo '<article class="fs-post-article">
<div class="fs-post-content-wrapper">
content after post 10 is here
</div>
</article>';
}
// close the loop WHILE
}
wp_reset_postdata();
// close post wrapper
echo '</div>';
//function for the Easy Load More plugin
load_more_button();
$myvariable = ob_get_clean();
return $myvariable;
// close the query IF
}
// ELSE it cant find ANY posts that match
else {
echo '<div class="fs-no-posts-found"><h4>We currently have no stories to show you.</h4></div>';
// close the query ELSE */
}
}
Would it be as simple as somehow checking the current_post
count AND the page number of the pagination? Since I supposedly know how many posts would show per page...