0

I'm using a custom post type on my WP website and also using bootstrap.

I want to list 5 custom post type entries on my home page, and I want the first 3 to have class col-md-4 in one row and in the next row 2 cpt entries to have the class col-md-6

At the moment I have a class col-md-4 and 2 items in the 2nd row are not centered nicely.

This is my current code:

<div class="container-fluid gray-section">
                <div class="container">
                    <div class="row">
                        <?php 
                        $projects = get_posts(array('post_type'=>'project','posts_per_page'=>5, 'order'=>'ASC'));
                        if ($projects) { ?>
                            <ul class="list-unstyled">
                                <?php foreach ($projects as $post) { setup_postdata( $post ) ?>
                                <li class="col-md-4 col-sm-6 col-xs-12 col-xxs-12 text-center ">
                                    <a class="highlights-item" href="<?php echo get_permalink(); ?>">
                                        <div class="highlights-container">
                                                    <?php the_post_thumbnail(); ?>
                                                    <span class="highlights-title">
                                                        <?php the_excerpt(); ?>
                                                    </span>
                                        </div>
                                    </a>
                                </li>
                                                    <?php } wp_reset_postdata(); ?>
                            </ul>
                                            <?php }
                                ?>  

                            </div> <!-- end row -->
                        </div> <!-- end container -->
                    </div> <!-- end container fluid --> 

Any suggestions how to fix this?

1

Try the code below.

<div class="container-fluid gray-section">
                            <div class="container">
                                <div class="row">
                                    <?php 
                                    $projects = get_posts(array('post_type'=>'project','posts_per_page'=>5, 'order'=>'ASC'));
                                    if ($projects) { ?>
                                        <ul class="list-unstyled">
       <?php $i = 0; ?>
                      <?php foreach ($projects as $post) { setup_postdata( $post ) ?>
        <?php if($i % 3 == 1) { ?>
        <li class="col-md-4 col-sm-6 col-xs-12 col-xxs-12 text-center ">
        <?php } else if ($i % 3 == 0 && $i > 0) { ?>
        <li class="col-md-6 col-sm-6 col-xs-12 col-xxs-12 text-center ">
        <?php } ?>**
                                                <a class="highlights-item" href="<?php echo get_permalink(); ?>">
                                                    <div class="highlights-container">
                                                                <?php the_post_thumbnail(); ?>
                                                                <span class="highlights-title">
                                                                    <?php the_excerpt(); ?>
                                                                </span>
                                                    </div>
                                                </a>
                                            </li>
    <?php $i++; ?>
                                                                <?php } wp_reset_postdata(); ?>
                                        </ul>
                                                        <?php }
                                            ?>  

                                        </div> <!-- end row -->
                                    </div> <!-- end container -->
                                </div> <!-- end container fluid --> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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