【发布时间】:2026-01-21 21:40:01
【问题描述】:
我目前正在使用以下代码来获取类别页面中的子类别并获取每个子类别的帖子。在它回显“span-12”(第 14 行)的部分,我希望它为每个其他子类别回显“span-12 last”。
我的代码:
<?php $cats = get_categories('child_of='.get_query_var('cat'));
foreach ($cats as $cat) :
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1, // max number of post per category
'category__in' => array($cat->term_id)
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) :
echo '<div id = "seasonBlock" class = "span-12" >';
echo '</br><h3 class = "seasonTitle" >'.$cat->name.'</h3>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php /*general loop output; for instance: */ ?>
<div id = "episodeBlock" >
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
aired <?php if(!function_exists('how_long_ago')){the_time('F jS, Y'); } else { echo how_long_ago(get_the_time('U')); } ?><br />
</div>
<?php endwhile; ?>
</div>
<?php else :
echo 'No Posts for '.$cat->name;
endif;
wp_reset_query();
endforeach; ?>
在这部分,(第 14 行)
echo '<div id = "seasonBlock" class = "span-12" >';
我想为每个其他类别回显,我希望它像这样回显
echo '<div id = "seasonBlock" class = "span-12 last" >';
我找到了一些用于添加和检查是否奇怪的代码,但这似乎不适用于此代码。
【问题讨论】:
标签: php wordpress foreach parent-child categories