• Hi,

    I thought that I understand the loop, but obviously I don’t.

    On many of my websites I use quite many loops.

    My first question is:
    What is the difference between the “2 Main Loop Techniques” – “get_posts()” and “query_posts()” ?
    Is there any difference at all?

    I think I’ve figured out some conflicts especially with conditional tags:
    I have one “Main-Loop” on my page that is displayed, let’s say it’s a single article (single.php).
    Under the single article I want to display some “Related Articles”. I do this with a simple “query_posts()” loop.

    <?php
    //first loop
    while (have_posts()) : the_post();
      // the content of the post
      the_content('');
    endwhile;
    
    //second loop
    rewind_posts();
    $related_query="cat=".$cat-var."&showposts=3";
    query_posts($related_query);
    while (have_posts()) : the_post();
      // the title of the related post
      the_title('');
    endwhile;
    ?>

    OK. Let’s assume that the post in Loop 1 and Loop 2 are in different categories. And let’s assume that I want to use the conditional Tag “in_category()” in the sidebar.

    I would expect the “in_category()” tag to output TRUE for the “ORIGINAL POST’S” category (Loop #1).

    But it outputs TRUE for the last post in Loop #3. This is comprehensible, but in my opinion not the proper solution.

    Is there any workaround for that?

    Of course, I could do something like

    $myposts = get_posts('numberposts=5&offset=1&category=1');
    foreach($myposts as $alternativepost) : ?>
        <li><a href="<?php echo $alternativepost->guid; ?>"><?php echo $alternativepost->post_title/?></a></li>
    <?php endforeach; ?>

    But as you see, I cannot use many template tags anymore with this workaround.

    It is a very tricky issue and I think I’m just starting. I hope, I expressed clearly what I want to show.
    It’s just about making a simple, “clean” loop without always affecting and overwriting the $post – variable.

  • The topic ‘Trying to understand The Loop and the $post – Variable’ is closed to new replies.