Wordpress显示the_content抓取错误帖子的内容。 为什么?

问题描述:

My problem is that I am displaying some posts from the category "events". Then a bit later on the same page I want to display a random post from the category "spiller" and that works fine. It gets a random post, shows the title, the thumbnail, but when I say show the_content (or the_excerpt), it shows all the content (or excerpts) of the posts in the category "events". Please help me solve this!

<div class="well span6 Padding10">
    <h4 class="titleFont MarginBottom20">KOMMENDE BEGIVENHEDER</h4>
    <?php
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $args  = array(
        'category_name' => 'events', // Change these category SLUGS to suit your use.
        'paged'         => $paged

    );
    query_posts( $args ); ?>
    <ul>
        <?php
        while ( have_posts() ) : the_post(); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
            </li>
        <?php endwhile; ?>
    </ul>
</div>

<div class="span6 well" style="height: 250px;"><h4 class="titleFont">SPILLER HIGHLIGHT</h4>
    <div class="row-fluid">
        <?php
        $args       = array(
            'numberposts'   => 1,
            'orderby'       => 'rand',
            'category_name' => 'spiller'
        );
        $rand_posts = get_posts( $args );
        foreach ( $rand_posts as $post ) : ?>

            <div class="span5"><?php the_post_thumbnail( array( 150, 150 ) ); ?></div>
            <div class="span6 MarginTop10">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <!-- THIS IS WHERE IT MESSES UP: --><?php the_content(); ?>
            </div>
        <?php endforeach; ?>
    </div>
</div>

我的问题是我正在显示“事件”类别中的一些帖子。 然后稍后在同一页面上,我想显示一个来自“spiller”类别的随机帖子,并且工作正常。 它会随机发布一个帖子,显示标题,缩略图,但是当我说show_content(或the_excerpt)时,它会显示“事件”类别中帖子的所有内容(或摘录)。 请帮我解决这个问题! p>

 &lt; div class =“well span6 Padding10”&gt; 
&lt; h4 class =“titleFont MarginBottom20”&gt; KOMMENDE BEGIVENHEDER&lt; / h4&gt  ; 
&lt;?php 
 $ paged =(get_query_var('paged'))?  get_query_var('paged'):1; 
 $ args = array(
'category_name'=&gt;'events',//更改这些类别SLUGS以适合您的使用。
'paged'=&gt; $ paged \  n 
); 
 query_posts($ args);  ?&gt; 
&lt; ul&gt; 
&lt;?php 
 while(have_posts()):the_post();  ?&gt; 
&lt; li&gt; 
&lt; a href =“&lt;?php the_permalink();?&gt;”&gt;&lt; strong&gt;&lt;?php the_title();  ?&gt;&lt; / strong&gt;&lt; / a&gt; 
&lt; / li&gt; 
&lt;?php endwhile;  ?&gt; 
&lt; / ul&gt; 
&lt; / div&gt; 
 
&lt; div class =“span6 well”style =“height:250px;”&gt;&lt; h4 class =“titleFont”&gt; SPILLER HIGHLIGHT&lt;  ; / h4&gt; 
&lt; div class =“row-fluid”&gt; 
&lt;?php 
 $ args = array(
'numberposts'=&gt; 1,
'orderby'=&gt;'  rand',
'category_name'=&gt;'spiller'
); 
 $ rand_posts = get_posts($ args); 
 foreach($ rand_posts as $ post):?&gt; 
 
&lt; div  class =“span5”&gt;&lt;?php the_post_thumbnail(array(150,150));  ?&gt;&lt; / div&gt; 
&lt; div class =“span6 MarginTop10”&gt; 
&lt; h4&gt;&lt; a href =“&lt;?php the_permalink();?&gt;”&gt;&lt;?  php the_title();  ?&gt;&lt; / a&gt;&lt; / h4&gt; 
&lt;! - 这就是它的重点: - &gt;&lt;?php the_content();  ?&gt; 
&lt; / div&gt; 
&lt;?php endforeach;  ?&gt; 
&lt; / div&gt; 
&lt; / div&gt; 
  code>  pre> 
  div>

First of all, you need to avoid using query_posts. It affects a lot of Wordpress Globals, and alters the Default Loop which - unless this is your specific intention - absolutely needs to be avoided as it can lead to performance issues as well.

Please look into substituting query_posts with WP_Query instead.

Apart from that, you need to reset your postdata, as well as set up your new postdata in the next loop.

Resetting The Query::

<?php 
while ( have_posts() ) : the_post(); 
?>
<li>
    <a href="<?php the_permalink(); ?>"><strong><?php the_title(); ?></strong></a>
</li>

<?php endwhile;wp_reset_query(); ?>

Set up:

foreach( $rand_posts as $post ) : setup_postdata($post); ?>

Reset The Postdata:

<?php endforeach;wp_reset_postdata(); ?>

Why does this need to be done?

Anytime you're using one of those handy Wordpress functions prefixed with "the_", that function is referencing the $post Global. query_posts changes that Global (as mentioned above) and if you plan on referencing that Global in a separate Loop, you need to be able to change it again on the fly.

Resetting your queries is just good general practice in making sure that all of your Globals are back to the Wordpress Defaults. But setup_postdata($post_object) is what actually allows us to change that Global to the current object in our custom loop.

The reason why WP_Query is so effective is that resetting the query is no longer essential as the WP_Query Loops are localized to that particular Object, and doesn't modify the Global $wp_query (which incidentally affects a LOT of other globals).

Here's some handy information about query_posts vs WP_Query that should explain things a little better for you.

Hops this helps.