首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Posts循环中的WordPress目标最后项

Posts循环中的WordPress目标最后项
EN

Stack Overflow用户
提问于 2014-05-09 09:52:05
回答 1查看 921关注 0票数 0

我想针对foreach循环中的最后一项,并添加一些类的。这可能吗?

这是我的密码:

代码语言:javascript
复制
<?php
$args = array(
    'posts_per_page'  => -1,
    'post_type'       => 'art',
    'cat'             =>  '6',
    'orderby'         => 'name',
    'order'           => 'ASC',
    'post_status'     => 'publish'
);
$posts_array = get_posts( $args );

foreach($posts_array as $post) : setup_postdata($post);
if ( has_post_thumbnail() ) {
    $title = str_replace(" ", "&nbsp;", get_the_title());
    $thumb = get_the_post_thumbnail($post->ID, 'exhibition-pre');
    if (has_post_thumbnail()) { ?>
        <div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs">
                <a href="<?php echo get_permalink() ?>">
                    <?php echo get_the_post_thumbnail($page->ID, 'art-thumb', array('class' =>       'max-img ')); ?>
                    <div class="imgcont"><?php echo the_title();?></div>
                </a>

        </div>
    <?php
    }
}
endforeach; ?>
<?php wp_reset_postdata(); ?>

最后,我希望:

代码语言:javascript
复制
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs CLEAR-Tablet>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs CLEAR-Screen>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs CLEAR-Tablet>...</div>
<div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs CLEAR-Screen>...</div>

诸若此类。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-09 10:50:15

正如已经说过的,您可能可以通过css实现这一点,但是如果您想要服务器端解决方案,请尝试如下所示:

首先要注意的有两件事:

  1. 考虑在这类事情上使用WP_Query而不是get_posts(),因为这是推荐的最佳实践
  2. 由于某种原因,您需要检查两次特写图像。

因此,如何在循环中每4/5项附加一个类。

代码语言:javascript
复制
//Step 1: Outside the loop setup a counter

$i = 1;

foreach($posts_array as $post) :

    setup_postdata($post);

    //Step 2: Once we're in the loop, setup our extra classes
    $classes = ' ';

    //for multiples of 4 
    if ( $i % 4 == 0) {
        $classes .= ' CLEAR-Tablet';
    }

    //for multiples of 5
    if ( $i % 5 == 0) {
        $classes .= ' CLEAR-Screen';
    }

    if ( has_post_thumbnail() ) {
        $title = str_replace(" ", "&nbsp;", get_the_title());
        $thumb = get_the_post_thumbnail($post->ID, 'exhibition-pre');



        //Step 3: Include our extra classes
        ?>

            <div class="grid-3 grid-tablet-4 grid-mobile-6 artythumbs <?php echo $classes; ?>">

                <a href="<?php echo get_permalink() ?>">
                    <?php echo get_the_post_thumbnail($page->ID, 'art-thumb', array('class' =>       'max-img ')); ?>
                    <div class="imgcont"><?php echo the_title();?></div>
                </a>

            </div>

        <?php

        //Step 4: Increment the counter (you might want to do this outside the if but I think it's better here.
        $i++;
    }

endforeach; ?>

<?php wp_reset_postdata(); ?>
  1. 在循环外设置一个计数器,我从1开始,因为我们从1开始计算行数。
  2. 使用模数运算符,我们可以判断我们是在4或5的倍数,并适当地设置一个类变量。
  3. 打印类变量
  4. 增加计数器
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23561581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档