Wordbar sidebar and loop php code mangle each other

I edited the single.php file to suit my needs and it works. I only stayed in the part of the loop in which the following:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
        <div class="entry">
        <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
        <?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>     </div>
    </div>
<?php endwhile; else: ?>

    <p>Sorry, no posts matched your criteria.</p>

<?php endif; ?>

      

It only displays the text as I want. The problem is that I am adding the following code to use as a sidebar in the template;

<?php query_posts('showposts=10'); ?>
 <?php while (have_posts()) : the_post(); ?> 
<a href="<?php the_permalink() ?>" rel="bookmark" title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br /> 
<?php endwhile;?> 

      

It should display the title of the last 10 posts. But now the loop also displays the latest (full0 10 messages, not just one permalink related message ... I think the variable or so is reused and should be quiescent. Note that in single.php you get the code first " sidebar "and then you get the loop code.

So why does Wordpress behave this way?

+2


a source to share


2 answers


The reason for this is that Wordpress is a nightmare maze of globals. query_posts()

is one of the worst offenders. If you check the documentation for this function, you will see that they should even warn you that they are mostly not using it:

Important note

The query_posts function is intended to be used to modify the main page Loop only. It is not intended as it means creating secondary loops on page. If you want to create separate Loops outside of the main one, you must create separate WP_Query objects and use them instead. Using query_posts on non-main loops can cause your main loop to get wrong and possibly display what you are not expecting.

They added some object oriented objects that you can now use, namely the WP_Query object (why they didn't update the "main" pages to get rid of the ridiculous "The Loop" is still questionable).



You will want to do something like this in the sidebar:

<?php
$recentPosts = new WP_Query();
$recentPosts->query('showposts=10');
while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

<a href="<?php the_permalink() ?>" rel="bookmark"
    title="Link to <?php the_title(); ?>">
<?php the_title(); ?></a><br /> 
<?php endwhile;?> 

      

Google on how to use WP_Query if you want some examples .

+5


a source


request ('showposts = 10'); while ($ recentPosts-> has_posts ()): $ RecentPosts-> the_post () ;? >

"

rel = "bookmark" title = "Link to">

reading u code putting in sidebar, u is trying to get the last 10 post titles to be displayed in the sidebar, right? if so, you can simply use this line:



`<?php wp_get_archives('title_li=&type=postbypost&limit=10'); ?>

      

+1


a source







All Articles