Editing a loop to show additional images

I'm sure this is straightforward but I don't have a lot of experience with php.

The following code puts 3 boxes across a page that are fed from the posts with a certain category. What I want to do it make it put 6 boxes across the page - a line of 3 then another line of 3 underneath. I changes the 'posts_per_page' =>' line to six and while it allows for 6 posts to be showed the last image falls on a 3rd line sort of like this:

X X X

X X

X

Any idea how to make it work?

Thanks!

<?php
$args = array(
             'category_name' => 'cafe',
     'post__not_in' => $slider_arr,
           'post_type' => 'post',
          'posts_per_page' => 3
             //'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
             );
query_posts($args);
$x = 0;
while (have_posts()) : the_post(); ?>
<?php if($x == 2) { ?>
<div class="home_blog_box home_blog_box_last">
<?php } elseif($x == 1) { ?>
<div class="home_blog_box home_second_blog_box">
<?php } else { ?>
<div class="home_blog_box">
<?php } ?>
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('featured-blog'); ?></a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div><!--//home_blog_box-->    
<?php $x++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="clear"></div>

Solutions

Try resetting $x to -1 once it reaches 2 (-1 because of the default increment at the end). The last item never gets the css class home_blog_box_last because $x just keeps increasing.

<?php if($x == 2) { ?>
<div class="home_blog_box home_blog_box_last">
<?php $x = -1;
} elseif($x == 1) { ?>
<div class="home_blog_box home_second_blog_box">
<?php } else { ?>
<div class="home_blog_box">
<?php } ?>

Also, this causes a problem (though you don't see it) on your second box in the middle row because it doesn't get the css class you think it does neither.

Tags: PHP / Wordpress / Loops

Similar questions

Register an additional image size for only some images in my media library
There is a type of post on my blog which requires one extremely large image (such that it can be zoomed). I don't want to register a new image size, because I only need a certain small subset of images to have this large image size. I have managed to generate this additional large image and it sits alongside the other three "generated" images in th...
WooCommerce CSV Image URL Creates Additional Images in Media Folder
I upload all images through the Media Library Drag and Drop Uploader with no problems. Every time I use a CSV file with several thousand products, I enter the image url for each product, which is located in the Media Library. Example: /wp-content/uploads/2018/08/oa901-essence-1oz.jpg As a result, the product page uses the image with the added -1 in...
WordPress add_image_size() creates additional images not defined
In the functions.php file of my wordpress theme (FoodRecipes), I find the following code. When I up upload a 932x603 image and view what size images were created, I find that most of those sizes were created (but not all), and some extra were created which I can't account for. Here are the exceptions... 1) The 940x'' image was NOT created. I'm gues...
Get the post permalink within the loop but without additional DB query
I am looking to create a custom 'directory' page which will link to several thousands of posts. Because of the large number I am trying to limit the number of DB queries. I notice that to link to each post, I am calling get_permalink() and this is creating an extra DB query for each post. Is there anyway to retrieve the post permalink without an ad...
wordpress loop with additional elements
I created post template for a specific category, where are thumbnail, title, excerpt and I added additional div to the template with custom content, which will be generated automatically, let it be div class = "custom". I also created a loop on the blog page which shows all post from this specific category. I know how to show thumbnail, title, exce...
Wordpress standard loop: additional post count with parameters
I am altering a template file of the Search & Filter Wordpress Plugin to show additional post counts. The plugin uses the standard Wordpress loop to query posts by certain parameters. The number of posts can be counted by using found_posts. Now I want to display a second post count that takes additional parameters into account, eg. post_status....

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.