WordPress.org

Ready to get started?Download WordPress

Forums

Is this dual loop in my tag.php ok to use? (3 posts)

  1. noonoo
    Member
    Posted 5 years ago #

    Hi,

    I have just put this dual loop in my tag.php, its working and from reading the documentaion i think ive done it properly but could somebody please check incase ive made an error or incase there is a better way to do it:

    <?php if (have_posts()) : ?>
    <?php $posts = query_posts($query_string . '&cat=-100&showposts=100'); while (have_posts()) : the_post(); ?>
    
    Loop 1 Stuff...
    
    <?php endwhile; ?>
    <?php rewind_posts(); ?>
    
    <?php $posts = query_posts($query_string . '&cat=100&showposts=100'); while (have_posts()) : the_post(); ?>
    
    Loop 2 Stuff...
    
    <?php endwhile; ?>
    <?php else : ?>
    
    My Error Stuff 404 Message...
    
    <?php endif; ?>

    The top loop is set to exclude a category and the second is set to include the category the first loop excluded.

    Adding or removing <?php rewind_posts(); ?> doesnt seem to make a difference but i left it there as i read it was required.

    Does it matter having $posts for each loop or do i need to name each loop differently im not sure how that works for query_posts.

    The showposts=100 is just there to make sure all the posts for the tag show on the one page, in reality there will probably only 10-15 posts per tag but again i read using a higher number is fine as it will just stop if it runs out of posts before it gets to 100 so is this ok also.

    Thanks in advance for any help or advice.

    Nick

  2. noonoo
    Member
    Posted 5 years ago #

    I just noticed a problem with <?php single_cat_title(); ?> not showing the tag title in the second loop, so i changed the loops to:

    <?php if (have_posts()) : ?>
    
    Cat Title Here...
    
    <?php $posts = query_posts($query_string . '&cat=-100&showposts=100'); while (have_posts()) : the_post(); ?>
    
    Loop 1 Stuff...
    
    <?php endwhile; endif; ?>
    
    <?php rewind_posts(); ?>
    <?php if (have_posts()) : ?>
    
    Cat Title Here...
    
    <?php $posts = query_posts($query_string . '&cat=100&showposts=100'); while (have_posts()) : the_post(); ?>
    
    Loop 2 Stuff...
    
    <?php endwhile; endif; ?>

    But it didnt seem to help, if i move <?php single_cat_title(); ?> after the <?php $posts = query_posts it shows the category title its getting the posts from rather than the tag title for the page.

    Thanks for any help

    Nick

  3. noonoo
    Member
    Posted 5 years ago #

    I realised a mistake i made i needed to use <?php single_tag_title(); ?> instead of <?php single_cat_title(); ?>, it all seems ok now but again if anybody knows of a better way to run two loops in the tag.php or the methods i mentioned above are incorrect please let me know.

    Thanks

    Nick

Topic Closed

This topic has been closed to new replies.

About this Topic