我撞了将近一天的头,但还是没能解决。我需要显示一个来自4个xx类别的帖子这里是我的代码
<?php
$cat_args = array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'include\' => \'44,45,46,51\'
);
$fcategories = get_categories($cat_args);
foreach($fcategories as $fcategory) {
echo \'<dl>\';
echo \'<dt> <a href="\' . get_category_link( $fcategory->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . \'" \' . \'>\' . $fcategory->name.\'</a></dt>\';
$post_args = array(
\'showposts\' => 1,
\'category\' => $fcategory->cat_ID
);
$fposts = get_posts($post_args);
foreach($fposts as $fpost) : setup_postdata($fpost); ?>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php
echo \'<dd class="view-all"> <a href="\' . get_category_link( $fcategory->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . \'" \' . \'>View all posts in \' . $fcategory->name.\'</a></dd>\';
echo \'</dl>\';
endforeach;
}
?>
每个想法似乎都很完美,除了。。。。它在所有类别中只显示一个帖子,而它应该在每个类别中显示一个帖子,以便更具体地显示它的显示方式
cat-1 cat-2 cat-3 cat-4(这是正常的)post-1 post-1 post-1(这是问题)
虽然应该是
cat-1 cat-2 cat-3 cat-4后1后2后3后4后
请帮忙
最合适的回答,由SO网友:mbajur 整理而成
我已在本地检查了此代码,下面是工作片段:
<?php
$cat_args = array(
\'orderby\' => \'name\',
\'order\' => \'ASC\',
);
$fcategories = get_categories($cat_args);
foreach($fcategories as $fcategory) {
echo \'<dl>\';
echo \'<dt> <a href="\' . get_category_link( $fcategory->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . \'" \' . \'>\' . $fcategory->name.\'</a></dt>\';
$post_args = array(
\'posts_per_page\' => 1,
\'cat\' => $fcategory->cat_ID
);
$fposts = query_posts($post_args);
while(have_posts()) : the_post(); ?>
<dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
<?php
echo \'<dd class="view-all"> <a href="\' . get_category_link( $fcategory->term_id ) . \'" title="\' . sprintf( __( "View all posts in %s" ), $fcategory->name ) . \'" \' . \'>View all posts in \' . $fcategory->name.\'</a></dd>\';
echo \'</dl>\';
endwhile;
}
wp_query_reset();
?>
<小时>
Original answer:<我认为问题可能在这里:
$post_args = array(
\'showposts\' => 1,
\'category\' => $fcategory->cat_ID
);
您正在使用
category
参数,应该是
cat
请尝试以下代码:
$post_args = array(
\'showposts\' => 1,
\'cat\' => $fcategory->cat_ID
);