-2

I am trying to select the fist post in a foreach loop but it always adds the additional HTML to every post. It works perfectly on another loop where the results are not grouped, but not in the foreach loop and I can't work out why (I'm not great with php)

<?php if(function_exists('FWP') && !FWP()->is_filtered): ?>

    <?php
    
        $century_posts = array();
    
        while ($query->have_posts()) {
            $query->the_post();
            $century = get_field('pm_century', get_the_id());
            $century_posts[$century][] = $post;

        }
    
        wp_reset_query();
    
        foreach ($century_posts as $century_post => $century_title) {
    ?>
          
          
        <?php
            
          foreach ($century_title as $listing => $single_listing) {
            setup_postdata($single_listing);
            $post_id = $single_listing->ID;
            $title = get_the_title($post_id);
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' ); 
        ?>
        
        
        <?php $nameclass = str_replace(" ", "", $title); ?>
        
              
              
              <div class="single-prime-minister <?php echo strip_tags(get_the_term_list( $post_id, 'party', ' ',', ')); ?> <?php echo $nameclass ?>">
                                    
                    <a href="<?php the_permalink($post_id); ?>" title="<?php echo $title ?>" aria-label="Find out more about <?php echo $title ?>"> 
                        <div class="pm-filter-profile-image">
                            <img src="<?php echo $image[0]; ?>" alt="" />
                        </div>
                    </a>

                                   
                    <div class="pm-filter-details-wrap">
                        <h4><?php echo $title ?></h4> 


                        <?php if( $wp_query->current_post == 0 && !is_paged() ) : ?>
                        <p>first item</p>
                        <?php else : ?>
                        <p>other items</p>
                        <?php endif; ?>


                    </div>

              </div>

              
              
        <?php
        }
          wp_reset_postdata();
          
        } ?>
        
<?php endif; ?>     

I have also tried

<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>

<?php if($postCount == 2) { ?>
  // SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
  // SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>

This just crashes everything but it maybe my placement of it

And since tried

<?php $i = 0;
$len = count($array);
foreach ($array as $item) {
    if ($i == 0) { ?>
        <p>first</p>
    <?php } else if ($i == $len - 1) { ?>
        <p>last</p>
    <?php }
    // …
    $i++;
} ?>

Which outputs nothing

Mr Toad
  • 202
  • 2
  • 12
  • 41
  • `if( $wp_query->current_post == 0` - that of course can't work here, because you are _not_ iterating over the actual query result (which is not even stored in `$wp_query`, but `$query` to begin with), but you are looping over your own _array_ here. – CBroe Aug 11 '23 at 13:27
  • [PHP How to determine the first and last iteration in a foreach loop?](https://stackoverflow.com/q/1070244/1427878) – CBroe Aug 11 '23 at 13:28
  • I did try a few tweaks, including changing it to $query I have also now tried the above (added to post) I'll keep trying different stuff – Mr Toad Aug 11 '23 at 14:06

0 Answers0