wordpress basic loop issues

Im having a problem where for some reason, a basic loop im trying to add to a sidebar keeps….keeps looping on interior pages (single and page .php ) but not on index.php.

on index.php (home page) is fine.

Read More

for example, i wanted the loop to populate an unordered lists line items, so i went about it like this:

      <div id="sideBarMain">
        <h2>title4sidebar</h2>
       <ul>     
             <?php 
            query_posts( array ( 'category_name' => 'left_sidebar', 'posts_per_page' => 4, 'orderby=menu_order' ) );
            if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li><a href="<?php the_permalink();  ?>"><?php the_title(); ?></a></li>
            <?php endwhile; endif; ?>
        </ul>

      </div><!-- sidebar ender -->

A few things are happening.
1. the MOST problematic is that when this loop is in place, it breaks the navigation and all. i click on a page and the page loads up with one of the list line items on the header…so if i click “about us” on the main nav, a page loads with “line item title” instead…..

also happens on any item clicked.

2 – is that on the (page/single.php pages), on the sidebar, it will post the first 4 posts with the category “left_sidebar” how it should be displayed, css in place and all but after the fourth line item, it will repeat the loops one more time.

3 – My sites content area has 2 columns, the sidebar on the left, and the main content area on the middle. On the part that’s being wrongfully repeated (after the loops repeats), when i checked with FireBug, on the repeated line items, its also taking the H tag properties from i assume, an h-tag thats next to it on another column. So the first 4 line-items come out fine with the styles given to them via CSS, link colors, rollovers etc, and the next buggy 4 line items come out un-styled, permalink also isnt being applied to them like the first 4 etc.

3 – i removed all html from that php loop like the divs and ul / lis etc so no css is being applied, this way only text come up from the loop but the same thing happens, Since i removed the html portion of the loop, the first 4 li’s come up with the standard default blue link text etc, and the following buggy 4 come up the same as before all big with htags attached….

Troubleshooting it all,
– theres only one loop on the page which stems from the sidebar.
– ive properly cleared all floats
– when checking on W3C for errors etc, there are none. green lights through and through.
– no errors on the php side of things. (ive checked every page and have errors enabled) on local server.

im lost.

Any ideas as to what can be happening?

NOTE: thinking ahead, although there is one loop ONLY on the sidebar.php page, once imported technically theres 2 loops which i guess is the sidebar wp loop, and the page.php / single.php loops which get the content.

is there anything different as of the latest wp maybe? Ive always done it like this and now i have problems……

any ideas? im lost.

Thanks in advanced.

Related posts

Leave a Reply

2 comments

  1. Your problem is two-fold:

    1) It is imperitive you stop using query_posts. There are many well-documented reasons as to why using this function needs to end, but I think this is the best explanation.

    2) You aren’t resetting your query. More on that in a minute.

    Going back to number 1, the problem with query_posts is that it affects the global $wp_query object. This would be fine if your intention was to modify the main loop, but that’s not what you’re trying to do here. What you need to use is WP Query. The syntax in creating a new Query Object is identical to query_posts, but the benefit is that you are no longer affecting the global instance of $wp_query.

    Resetting your query also ensures that no other sections of code utilizing a WordPress Query are going to be affected by your Query Object.

    Try changing your code example to this:

    <?php 
    $query = new WP_Query(array('category_name'=>'left_sidebar','posts_per_page'=>4,'orderby=menu_order'));
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
    ?>
    <li><a href="<?php the_permalink();  ?>"><?php the_title(); ?></a></li>
    <?php
    endwhile;endif;
    wp_reset_postdata();
    ?>
    
  2. There might be some filters in your functions.php that interact with these functions. Take a look there and see if you want find something.

    You should also try using <?php wp_reset_query(); ?> before and after your loop, so you make sure that it doesn’t interact with the others.