开发者

wordpress sidebar and loop php code distorts each other

I've edited the single.ph开发者_如何学编程p to suit my needs and it works. I only left in the part of the loop in in which is as follows:

<?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 displayes the text, like I want it to. The problem I get is when I add the following code to be used as the 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 is displaying the latest (full0 10 posts instead of just the one post that belongs to the permalink... I think a variable or so is being reused and needs to be rest.. Note that in the single.php first you get the 'sidebar' code, and then you get the 'loop' code.

So why is wordpress behaving this way?


The reason this happens is because Wordpress is a nightmarish maze of global variables. query_posts() is one of the worst offenders. If you check the documentation for this function, you'll see that they even have to warn you to basically not use it:

Important note

The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should create separate WP_Query objects and use those instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

They've added some object oriented stuff that you can use now instead, namely the WP_Query object (why they haven't revamped the "main" pages to get rid of the ridiculous "The Loop" stuff yet is questionable).

You're going to 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 around about how to use WP_Query if you want more examples.


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

"

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

reading the code u putting in the sidebar, u are trying to get the last 10 titles of posts to show in sidebar , right ? if so u can just use this line :

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜