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.
Add
wp_reset_query();
after your loop to prevent other loops on the page (for example, the navigation) to break.Does this also fix your other problems?
reference: http://codex.wordpress.org/Function_Reference/query_posts#Usage
Don’t use
query_posts
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. Usepre_get_posts
to alter the main loop.For your case, creating additional loops in templates, use
WP_Query
:1) Yes, this will break navigation because
query_posts
overwrites the main page Loop. Don’t usequery_posts
for secondary Loops– really I can’t think of a good reason to use it ever. Use a newWP_Query
object (my preference) or a function likeget_posts
The other problems may be related but your descriptions are confusing.