“每个其他”的不同输出Foreach 语句?

发布于 2025-01-05 20:29:35 字数 1505 浏览 0 评论 0原文

我目前正在使用以下代码来获取类别页面中的子类别并获取每个子类别的帖子。在它回显“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小镇女孩 2025-01-12 20:29:35

使用布尔变量。在循环的每次迭代结束时将其设置为其相反值:

//pseudo code
if (b) 
    outputFirst();
else 
    outputSecond();
b = !b;

Use a boolean variable. At the end of each iteration of the loop set it to its oposite value:

//pseudo code
if (b) 
    outputFirst();
else 
    outputSecond();
b = !b;
老子叫无熙 2025-01-12 20:29:35

您可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文