【问题标题】:WordPress target last Item in Posts foreach loopWordPress 将 Posts foreach 循环中的最后一项作为目标
【发布时间】:2014-05-09 09:52:05
【问题描述】:

我想在 foreach 循环中定位最后一个项目并在那里添加一些类。那可能吗?

这是我的代码:

<?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(); ?>

我想拥有到底:

<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>

等等。

【问题讨论】:

  • 您想要最后一个帖子还是每 4/5 个帖子?
  • 我的愿望是每 4 天/ 5 天 ;-)
  • 您能否通过nth-child 实现您的目标?通过 CSS 或 jQuery?

标签: php css wordpress loops foreach


【解决方案1】:

如前所述,您可能可以通过 css 执行此操作,但如果您想要服务器端解决方案,请尝试以下操作:

首先要注意两点:

  1. 考虑使用WP_Query 而不是get_posts() 进行此类操作,因为这是推荐的最佳做法
  2. 出于某种原因,您对特色图片进行了两次检查。

那么,如何在循环中每隔 4/5 个项目附加一个类。

//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. 增加计数器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-04
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2021-04-05
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多