PHP

How to split your WordPress posts in different rows and columns

watchLast Updated:

bookmarkWritten by: Yakovos Frountas

There is a simple and effective way to split your bunch of posts inside your theme. You will need a simple get_posts() or WP_Query functions and that’s all!

First we need to search for our posts

Prism Shortcode Error: could not get remote content. HTTP response code 404

You fill the category or category_name with the category which from you want to display your posts.

The next step is to create the foreach function inside of it we can extract the data of the get_posts. To help ourselves we setup a counter $i to count the posts one by one. After that you need an if statement to define how many posts you need to display in column one. In my example i have 3. Next process is to gather all the ids of posts that have been displayed in an array. Now we can display for example the title of each post.

Prism Shortcode Error: could not get remote content. HTTP response code 404

Final step is to pass in another row or column the remaining posts. So we have to do again one simple WP_Query and to run foreach loop one more time in order to display the posts.

Prism Shortcode Error: could not get remote content. HTTP response code 404

And the whole final code should be looking like this:

Prism Shortcode Error: could not get remote content. HTTP response code 404

 

 

Leave a Comment

Ready to get started?

Contact us today to learn how our customized plans can help grow your business.