Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress在Posts foreach循环中的目标最后一项_Php_Css_Wordpress_Loops_Foreach - Fatal编程技术网

Php WordPress在Posts foreach循环中的目标最后一项

Php WordPress在Posts foreach循环中的目标最后一项,php,css,wordpress,loops,foreach,Php,Css,Wordpress,Loops,Foreach,我想把foreach循环的最后一项作为目标,并在那里添加一些类。可能吗 这是我的代码: <?php $args = array( 'posts_per_page' => -1, 'post_type' => 'art', 'cat' => '6', 'orderby' => 'name', 'order' => 'ASC', 'post_

我想把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>
。。。
...
...
...
...

等等。

如前所述,您可能可以通过css实现这一点,但如果您需要服务器端解决方案,请尝试以下方法:

首先要注意两件事:

  • 考虑使用而不是
    get_posts()
    来处理这类事情,因为这是推荐的最佳实践
  • 出于某种原因,您对一张特色图片进行了两次检查
  • 那么,如何在循环中每隔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:在循环外部设置计数器
    $i=1;
    foreach($posts\u数组作为$post):
    设置_postdata($post);
    //第二步:一旦我们进入循环,就设置额外的类
    $classes='';
    //对于4的倍数
    如果($i%4==0){
    $classes.='CLEAR Tablet';
    }
    //对于5的倍数
    如果($i%5==0){
    $classes.='清除屏幕';
    }
    如果(具有\u post\u缩略图()){
    $title=str_replace(“,”,获取_title());
    $thumb=获取“发布”缩略图($post->ID,“展览前”);
    //第三步:加入我们的额外课程
    ?>
    
    你想要最后一篇还是每4/5篇文章?我的愿望是每4/5篇;-)你能通过CSS或?