簡體   English   中英

WordPress將Posts foreach循環中的最后一個項目作為目標

[英]WordPress target last Item in Posts foreach loop

我想在foreach循環上定位最后一個Item,並在其中添加一些類。 那可能嗎?

這是我的代碼:

<?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執行此操作,但是如果您想要服務器端解決方案,請嘗試以下操作:

首先要注意兩件事:

  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. 打印出class變量
  4. 增加計數器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM