dthok9648 2013-01-15 14:37
浏览 63
已采纳

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>
  • 写回答

1条回答 默认 最新

  • dousi8237 2013-01-15 14:49
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的