【问题标题】:How do I get wordpress to override a previous posts query?如何让 wordpress 覆盖以前的帖子查询?
【发布时间】:2009-03-03 20:27:20
【问题描述】:

我的页面顶部有一个类别列表,通常应该在其下方列出帖子。类别列表是使用以下方法创建的:

<?php $display_categories = array( 4, 7, 8, 9, 21, 1); $i = 1;
    foreach ( $display_categories as $category ) { ?>
        <div>
            <?php single_cat_title(); ?> //etc
        </div>
    <?php } 
?>

但是,这似乎使帖子循环按类别排序帖子。我希望它忽略类别排序和按日期降序排序。我创建了一个新的 WP_Query 因为根据文档你不能使用 query_posts() 两次,所以以防万一。

<?php $q = new WP_Query( "cat=-1&showposts=15&orderby=date&order=DESC" );
    if ( $q->have_posts() ) : 
        while ( $q->have_posts() ) : $q->the_post(); ?>
            the_title(); // etc
        endwhile; 
    endif; 
?>

但是,这个仍然似乎是按类别排序(与上面的列表相同)然后按日期排序,而不是按日期排序。

【问题讨论】:

  • 我有阳光...阴天...
  • 我希望我有一个页面。我所拥有的只是形成连贯句子的能力。

标签: php wordpress


【解决方案1】:

我以前也遇到过这个问题。

试试这个:

<?php
    global $post;
    $myposts = get_posts( 'numberposts=5' );
    foreach( $myposts as $post ) : setup_postdata( $post ); ?>
        <div <?php post_class(); ?>>
            <div class="title">
                <h2>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h2>
                <p class="small"><?php the_time( 'F j, Y' ); ?> by <?php the_author(); ?></p>
             </div>
             <?php the_excerpt(); ?>
         </div>
     <?php endforeach; 
 ?> 

重要的一行是global $post;

这应该会重置您的全局查询。 setup_postdata($post) 方法是您访问 the_author()the_content() 等函数所必需的。

-克里斯

【讨论】:

    【解决方案2】:

    我对 wordpress 没有任何经验,但有几种可能:

    1. 您在调用query_posts() 的字符串中定义了两次“order”参数,我不知道这是否会导致问题。
    2. 同样,“show”不是有效参数,您可能一直在寻找“showposts”。

    这里描述了参数及其效果:http://codex.wordpress.org/Template_Tags/query_posts#Parameters

    【讨论】:

    • 嗨,感谢您注意到这些。我已经更新了代码(和上面的帖子),但它似乎仍然无法解决问题。
    【解决方案3】:

    query_posts 有时很挑剔。尝试这样的事情,看看它是否有效:

    query_posts(array('category__not_in'=>array(1),
                      'showposts'=>15,
                      'orderby'=>date,
                      'order'=>DESC));
    

    既然这不是问题,请尝试将 update_post_caches($posts) 添加到第二个循环中,如下所示:

    <?php $q = new WP_Query("cat=-1&showposts=15&orderby=date&order=DESC");
    if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); update_post_caches($posts); ?>
    the_title(); // etc
    endwhile; endif; ?>
    

    应该是这个solves some plugin problems

    【讨论】:

    • 感谢您的回答!不过,这似乎没有任何区别。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多