1

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

<?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; 
?>

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

4

3 回答 3

2

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

试试这个:

<?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()

-克里斯

于 2009-03-04T00:18:34.687 回答
0

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; ?>

据说这解决了一些插件问题

于 2009-03-03T20:55:50.960 回答
0

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

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

此处描述了参数及其影响:http://codex.wordpress.org/Template_Tags/query_posts#Parameters

于 2009-03-03T21:10:02.750 回答