“每个其他”的不同输出Foreach 语句?
我目前正在使用以下代码来获取类别页面中的子类别并获取每个子类别的帖子。在它回显“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" >';
我想回显每个其他类别的foreach,我希望它像这样回显
echo '<div id = "seasonBlock" class = "span-12 last" >';
我找到了一些用于添加和检查它是否奇怪的代码,但这似乎不适用于这段代码。
I'm currently using the following code in order to get subcategories in a category page and getting the posts foreach subcategory. In the part where it echos "span-12" (line 14) i want it to echo "span-12 last" for every other subcategory.
my code:
<?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; ?>
In this part, (line 14)
echo '<div id = "seasonBlock" class = "span-12" >';
i want to echo for every other category foreach, i want it to echo like this
echo '<div id = "seasonBlock" class = "span-12 last" >';
I found some code for adding and checking if it is odd, but that doesn't seem to work for this code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用布尔变量。在循环的每次迭代结束时将其设置为其相反值:
Use a boolean variable. At the end of each iteration of the loop set it to its oposite value:
您可以使用 css 修饰符的 :first-child 和 :last-child 来仅使用 css 来完成您想要的操作。
请参阅:http://www.quirksmode.org/css/firstchild.html
You can use the css-modifier's :first-child and :last-child to accomplish what you want using only css.
See this: http://www.quirksmode.org/css/firstchild.html