我试图在小部件中只显示某个类别的帖子。我尝试了几种不同的方式,但它仍然显示所有类别的帖子。这是我目前的代码:
$posts = get_posts(array (\'category\'=>6, \'numberposts\'=>$number, \'order\'=>\'DESC\', \'orderby\'=>\'date\'));
foreach ($posts as $post){
setup_postdata($post);....
谢谢。我试图在小部件中只显示某个类别的帖子。我尝试了几种不同的方式,但它仍然显示所有类别的帖子。这是我目前的代码:
$posts = get_posts(array (\'category\'=>6, \'numberposts\'=>$number, \'order\'=>\'DESC\', \'orderby\'=>\'date\'));
foreach ($posts as $post){
setup_postdata($post);....
谢谢。你试过抄本上的例子吗?
<?php
global $post;
$args = array( \'numberposts\' => $number, \'category\' => 6, \'order\' => \'ASC\', \'orderby\' => \'post_date\' );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
这是一个经过修改的版本,应该适合您的需要。您是否也确定该类别存在?Edit:
<?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(\'showposts=\' . $numberposts . \'&cat=6&orderby=date&order=ASC\'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<!-- your content -->
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp; ?>
我猜你有两个循环,也许以前有一个。尝试使用wp_reset_query()
在或之前rewind_posts()
.
我正在使用wp\\u list\\u类别,如下所示:<?php //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin) $taxonomy = \'news_cat\'; $orderby = \'name\';