• Resolved rothworks

    (@rothworks)


    Hello,

    I am in the process of building a site for a client. I am running across issues for the front-page template I have created.

    I have 5 loops on the front-page.
    (recent activity, campaigns, news, etc..)

    For the first loop I used:

    [please use the code button to mark your code]

    <!– the loop for recent activity –>
    <?php query_posts(‘posts_per_page=3’); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    $do_not_duplicate[] = $post->ID;?>

    Then for each subsequent loop I use
    <?php query_posts(‘category_name=giveo-campaigns&posts_per_page=1’); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    if (in_array($post->ID, $do_not_duplicate)) continue; ?>

    It seems to work for the most part. However, some of the loops do not display posts at all. If I set the first query to:

    “array(‘posts_per_page’=> ‘3’, ‘orderby’ => ‘rand’)”
    Then refresh the page, it will sometimes display all of the posts correctly so there must be an issue with reseting $do_not_duplicate?
    I have tried unset($do_not_duplicate) to no avail.

    It may just be that it is 4 a.m. but I cannot seem to find the issue. Any help would be appreciated. The site is at https://pathfindersolutions.org/wordpress

    Thanks,

    Mike

Viewing 2 replies - 1 through 2 (of 2 total)
  • your method simply jumps over the post if the post is duplicate.
    with only one post, that is deadly.

    use the ‘array method’ for ‘not duplicating posts’

    https://codex.www.ads-software.com/The_Loop#Multiple_Loops_in_Action

    Thread Starter rothworks

    (@rothworks)

    Great thanks! – for anyone that is wondering

    I had to change the way the second and subsequent loops were queried from:

    <?php if (have_posts()) : while (have_posts()) : the_post();
    if (in_array($post->ID, $do_not_duplicate)) continue; ?>

    to:

    <?php query_posts (array('post__not_in'=>$do_not_duplicate, 'category_name'=>'stories', 'posts_per_page'=>'1')); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    All seems to be working correctly now.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Multiple Loops with recent activity category issues’ is closed to new replies.