1

看来,当我需要对帖子进行分类时,标准的 WP 循环可以正常工作。当在 category.php 中使用时,它只显示添加到某个类别的帖子,例如:我有两个类别,电影和音乐,我有帖子分配给每个类别,我也有帖子分配给这两个类别. 如果是这样,分配给两个类别的帖子完美地显示在两个类别中,而分配给一个类别的帖子仅显示在一个类别页面上。我尝试按日期或 ID 对它们进行排序,但使用 query_posts 不起作用。它导致在所有类别页面上显示所有帖子,除了它们按特定类别分组的形式。有没有办法以不同的方式对它们进行排序?

编辑:

这是标准循环,一切正常。正如我上面写的,虽然有“电影”类别页面,但仅分配给“电影”类别的帖子会显示在该页面上,每个类别页面都是如此。但我认为它是按日期和降序排序的。

<div class="category_info"><?php echo category_description(); ?></div>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="categorymain">
<h4 class="posttitle"><?php the_title(); ?></h4>
<?php echo intro_text(250); ?>
<a href="<?php the_permalink() ?>" class="readmore">read more »</a>
</div><!--end categorymain-->
<?php endwhile; endif; ?>

但是,当我用

<?php query_posts('order=ASC'); ?>
//...above code here
<?php wp_reset_query();?>

一切都毁了。在“电影”类别页面上,会显示“电影”类别的帖子,但也会显示与“电影”无关的所有其他类别的帖子,这在每个类别页面上都会发生。我希望我说清楚了 :) 哦,顺便说一句,所有内容都在 category.php 中编码。向大家问好:)


编辑

我找到了一个无缝的解决方案,这里是:

<?php global $wp_query;
$args = array_merge( $wp_query->query, array( 'category__in' => array(get_query_var('cat')), 'order' => 'DESC', 'orderby' => 'title' ));
query_posts( $args );
?>
4

2 回答 2

0
$categories = get_the_category();
query_posts('order=ASC&cat='.$categories[0]); ?>
//...above code here
<?php wp_reset_query();?>

我建议您阅读以下内容: http ://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

并查看获取类别功能:http ://codex.wordpress.org/Function_Reference/get_the_category

于 2013-01-21T12:55:02.123 回答
-2
<?php if (have_posts()) : ?>
<?php query_posts('cat=1&posts_per_page='.get_option('posts_per_page')); ?>
于 2013-01-15T17:52:36.690 回答