1

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.

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.

1

3 Answers 3

3

Add wp_reset_query(); after your loop to prevent other loops on the page (for example, the navigation) to break.

<?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;
    wp_reset_query(); // reset query
?>

Does this also fix your other problems?

reference: http://codex.wordpress.org/Function_Reference/query_posts#Usage

8

Don't use query_posts

query_posts() is meant for altering the main loop. It does so by replacing the query used to generate the main loop content. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result.

Despite the Codex telling you it's only for altering the main loop, I would go further and say there's no reason to use query_posts, ever. Use pre_get_posts to alter the main loop.

For your case, creating additional loops in templates, use WP_Query:

$args = array(
    'category_name' => 'left_sidebar',
    'posts_per_page' => 4,
    'orderby=menu_order'
);

$sidebar_query = new WP_Query( $args );

if ( $sidebar_query->have_posts() ) :
    while ( $sidebar_query->have_posts() ) :
        $sidebar_query->the_post();
        <li><a href="<?php the_permalink();  ?>"><?php the_title(); ?></a></li>
    endwhile;
endif;

wp_reset_postdata();
1
  • WP_Query is the way to go for sure. Apr 23, 2014 at 2:13
6

1) Yes, this will break navigation because query_posts overwrites the main page Loop. Don't use query_posts for secondary Loops-- really I can't think of a good reason to use it ever. Use a new WP_Query object (my preference) or a function like get_posts

$myquery = new WP_Query( 
  array ( 
    'category_name' => 'left_sidebar', 
    'posts_per_page' => 4, 
    'orderby=menu_order' 
  ) 
);
if ( $myquery->have_posts() ) : 
  while ( $myquery->have_posts() ) : 
    $myquery->the_post(); ?>
    <li><a href="<?php the_permalink();  ?>"><?php the_title(); ?></a></li><?php   
  endwhile; 
endif;

The other problems may be related but your descriptions are confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.