I am working on a loop where I want the content (from 2 separate categories) to be displayed like this:
1 large format post, 1 random product, 2 small format posts, 1 feature post ...repeat.
In order to achieve this I have written a template for the whole page - with a parent loop (yet to be written), then 4 templates for each nested loop ( - large/random/small/feature). The loop templates are loaded into the parent page using
include(locate_template('looptemplate.php')); - I need to use this method so I can pass a variable (
$do_not_duplicate) to each loop.
My problem is that I cannot get
numberposts to reset after each loop template, so if the large-format template uses the argument
numberposts => 1 then in order to make the small-format template show 2 posts it needs to use
numberposts => 3. This is fine for the first iteration of the parent loop, but it is not repeatable.
Here is the code for the parent page template (NB I have not yet written the parent loop because I'm still trying to solve this problem with
<?php $do_not_duplicate = array(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php include(locate_template('largenews.php')); ?>
<?php include(locate_template('productflash.php')); ?>
<?php include(locate_template('smallnews.php')); ?>
<?php include(locate_template('featurepost.php')); ?>
</div><!-- #content -->
And here is the code for largenews.php. (smallnews.php is very similar but want to show 2 more posts)
<?php $args = array( 'numberposts' => 1, 'category' => '35,1052'); $largepost = get_posts($args); foreach( $largepost as $post ) : setup_postdata($post); if (in_array($post->ID, $do_not_duplicate)) continue; array_push($do_not_duplicate, $post->ID); //Code to format post endforeach; wp_reset_postdata(); ?>
Now I have already tried every combination of
wp_reset_query that I can imagine, but the
numberposts from the first loop is still being counted in the
numberposts of the second loop (and any further iterations that will appear).
I have also tried to use
$largepost->wp_reset_postdata(); but I get this : "Fatal error: Call to a member function wp_reset_postdata() on a non-object".
Apologies for the length of this question but I hope I have made my problem clear, and included all the relevant info. Apologies to for the wonky code formatting, I couldn't get it to work any other way!