简体   繁体   中英

How to add a specific class to the first post in a custom loop in Wordpress

first post here so be patient :) I'll try to explain my problem as clear as i can, to be relevant to other people who might want the same problem as mine.

I'd like to obtain two results in my Wordpress child theme (based on Bootstrap, Wordpress Bootstrap Master by 320, to be precise):

  1. Create a double structure with two different loops , the first one with the most recent X posts (4, for the precision) and a specific formatting, and the second loop with the remaining posts and another specific formatting
  2. Add a specific category to the first post in the first loop leaving the other posts untouched

To be clear, my aim is to create a Bootstrap carousel with the first 4 posts of my Loop, and then style the other as a normal blog.

As for now, I've been able to create a custom loop with the first 4 posts with this code:

 <div class="carousel-inner">
                   <?php global $post; // required
                   $args = array('posts_per_page'=>4);
                   $custom_posts = get_posts($args);
                   foreach($custom_posts as $post) : setup_postdata($post); ?>
                   <div class="item"> 
                   <?php the_post_thumbnail('large'); ?>
                      <div class="carousel-caption">
                        <?php the_title(); ?>
                      </div>
                    </div>
                   <?php endforeach; ?>
                  </div>

Then I'm going to create a second loop "offsetting" the first 4 posts. There obviously is a cleaner solution I'm missing, as I've used it in the past, so if you have some advice I won't complain.

As regards the second problem, the most complex one, I've made some research and I've found a huge amount of different solutions. None of them is working, or maybe I'm not adapting them to the code above in the right way (beware, I'm only an amateur coder and I'm prone to errors).

My last try has been this one:

 <div class="carousel-inner">
                   <?php global $post; // required
                   $args = array('posts_per_page'=>4);
                   $custom_posts = get_posts($args);
                   $isFirst = true;
                   foreach($custom_posts as $post) : setup_postdata($post); ?>
                   <?php echo '<div class="item'; ?> 
                   <?php if ( $isFirst = true ) {
                    echo ' active';
                    } else {
                    echo '';
                   } ?>
                   <?php echo '">'; ?>
                   <?php $isFirst = false; ?>
                   <?php the_post_thumbnail('large'); ?>
                      <div class="carousel-caption">
                        ...
                      </div>
                    </div>
                   <?php endforeach; ?>
                  </div>

Of course, I tried to no avail.

Can you help me here with this? I'm an avid learner, and I would like to understand WTF is wrong with me! ;)

Thanks in advance, guys and girls! :)

Claudio

The following line needs to use the comparison operator . You are currently constantly assigning true to $isFirst , instead of checking it's actual value.

if ( $isFirst = true )

should be:

if ( $isFirst == true )

You can read more about them in the PHP documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM