0

I'm aiming to create a layout where the first post is full-width in the loop, all the posts after are 50%.

I created this code:

<?php if ( $query->have_posts() ) : ?>

                                <!-- begin loop -->
                                <?php $i=0; ?>
                                <?php while ( $query->have_posts() ) : $query->the_post();  $i++  ?>

                                <?php
                                if ( $i = 1) { 
                                
                                
                                ?>

                                <article class="col-12">
                                    <div class="item-inner-main">
                                        <div class="image-blocks-main">
                                            <a href="<?php the_permalink(); ?>">
                                                <img src="<?php the_post_thumbnail_url( 'owl-size' ); ?>">

                                        </div>
                                        <div class="text-holder-main text-holder-main-desktop-position">
                                            <time class="time "><span
                                                    class="year"><?php the_time( 'M.j' ); ?></span><span
                                                    class="lightorange"><?php the_time( 'H:i' ); ?></span></time>
                                            <h2><?php the_title(); ?>
                                            </h2>
                                        </div>
                                    </div>
                                    </a>
                                    <h1><?php echo $i; ?></h1>
                                </article>



                                <?php 
                                } if ($i >= 2) {  ?>
                                <article class="col-6">
                                    <div class="item-inner-main">
                                        <div class="image-blocks-main">
                                            <a href="<?php the_permalink(); ?>">
                                                <img src="<?php the_post_thumbnail_url( 'owl-size' ); ?>">


                                        </div>
                                        <div class="text-holder-main">
                                            <time class="time "><span
                                                    class="year"><?php the_time( 'M.j' ); ?></span><span
                                                    class="lightorange"><?php the_time( 'H:i' ); ?></span></time>
                                            <h2 class="entry-title-main item-main-half">
                                                <?php the_title(); ?>
                                            </h2>
                                        </div>
                                    </div>
                                    </a>
                                </article>
                                <h1><?php echo $i; ?></h1>
                                <?php } 
                                
                                ?>



                                <?php endwhile; ?>

However, when I test this code, all the posts output the number 1. I don't understand why the $i is not incrementing. Any ideas?

Thanks!

1 Answer 1

1

You're reassigning 1 to $i on every loop:

if ( $i = 1) { 

This should be

if ( $i == 1) { 

or even

if ( $i === 1) { 

which will also compare the post type, which is best practice.

In PHP, = is assignment, == is comparison:

1
  • Thank you so much! Can't believe I missed that ^^.
    – Pbalazs89
    Jan 15, 2022 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.