Implementing Custom Grid Layouts in WordPress Themes
Posted on 17th June 2023
The default WordPress themes use a grid layout to display content on the page. However, you can create custom grid layouts for your WordPress themes. In this article, we will show you how to create custom grid layouts for your WordPress themes.
Creating a Custom Grid Layout
First thing you need to do is create a new file in your WordPress theme. You can name it whatever you like, but we will name it “grid.php” for this example.
Next, you need to edit your “grid.php” file and add the following code:
This code tells WordPress that this is a custom grid layout. Now you can add your own HTML, CSS, and PHP code to this file to create a custom grid layout.
Adding CSS to Your Grid Layout
The next thing you need to do is add CSS to your grid layout. You can do this by adding a “style.css” file to your WordPress theme.
In your “style.css” file, you need to add the following CSS code:
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 20px;
}
.grid-item {
background: #fff;
border: 1px solid #ccc;
padding: 20px;
}
This CSS code will create a 3-column grid layout with 20px of space between each column.
Adding PHP Code to Your Grid Layout
Now that you have added the HTML and CSS for your grid layout, you can now add PHP code to it.
In your “grid.php” file, you need to add the following PHP code:
‘post’,
‘posts_per_page’ => 9
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
echo ‘
while ( $query->have_posts() ) {
$query->the_post();
echo ‘
the_title();
echo ‘
‘;
}
echo ‘
‘;
}
This PHP code will display the titles of your latest 9 blog posts in a 3-column grid layout.
Adding a Custom Grid Layout to Your WordPress Theme
Once you have created your custom grid layout, you need to add it to your WordPress theme.
You can do this by editing your “theme.php” file and adding the following code:
<?php
// Add custom grid layout
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup() {
add_theme_support( 'custom-grid-layout' );
}
This code will add your custom grid layout to your WordPress theme.
Adding a Grid Layout to Your WordPress Theme
Once you have created your custom grid layout, you need to add it to your WordPress theme.
You can do this by editing your “theme.php” file and adding the following code:
<?php
// Add grid layout to WordPress theme
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup() {
add_theme_support( 'grid-layout' );
}
This code will add a grid layout to your WordPress theme.
WordPress Theme Development
If you want to learn more about WordPress theme development, we have a few articles that can help you:
- An Introduction to WordPress Theme Development
- An Introduction to WordPress Theme Customization
- How to Create a Custom WordPress Theme
As we saw in the previous article, there are a few WordPress functions we can use to control the output of our grid layout. In this article, we’ll look at how we can use some of these functions to create a custom grid layout.
We’ll start by creating a new file called “grid-layout.php” in our theme’s folder. In this file, we’ll add the following code:
In this code, we’ve created a new WordPress Template called “Grid Layout”. This template can be selected when creating a new page in WordPress.
We’ve also included the standard WordPress code for outputting a loop of posts. However, we’ve removed the code that normally outputs the post title and featured image. We’ll be replacing this with our own custom code.
Next, we’ll add the following code to our “grid-layout.php” file. This code will output the post title and featured image for each post in our grid:
<?php
// Define our grid variables
$grid_columns = 2;
$grid_rows = 2;
// Output our grid header
echo '
echo ‘
‘;
// Output our grid items
for ( $i = 1; $i <= $grid_rows; $i++ ) {
for ( $j = 1; $j <= $grid_columns; $j++ ) {
echo '
echo ‘
‘ . get_the_title() . ‘
‘;
echo ‘
‘;
echo ‘
‘;
}
}
// Close our grid
echo ‘
‘;
?>
In this code, we’ve defined our grid layout to have two columns and two rows. We’ve then used a loop to output the post title and featured image for each post in our grid.
If we save this file and view our “Grid Layout” page in WordPress, we should see something like this:
As you can see, our grid layout is now working. However, it’s not very flexible. If we want to change the number of columns or rows, we have to edit our code.
It would be much better if we could change the number of columns and rows from the WordPress admin area. We can do this by adding some new settings to our “grid-layout.php” file:
<?php
// Define our grid variables
$grid_columns = get_theme_mod( 'grid_columns', 2 );
$grid_rows = get_theme_mod( 'grid_rows', 2 );
// Output our grid header
echo '
echo ‘
‘;
// Output our grid items
for ( $i = 1; $i <= $grid_rows; $i++ ) {
for ( $j = 1; $j <= $grid_columns; $j++ ) {
echo '
echo ‘
‘ . get_the_title() . ‘
‘;
echo ‘
‘;
echo ‘
‘;
}
}
// Close our grid
echo ‘
‘;
?>
In this code, we’ve replaced our hard-coded grid variables with calls to the WordPress “get_theme_mod” function. This function retrieves values from the WordPress database.
We’ve also added some default values for our grid variables. These default values will be used if no values have been saved in the WordPress database.
Now, if we go to the “Appearance > Customize” page in the WordPress admin area, we should see our new grid settings:
We can now change the number of columns and rows in our grid from the WordPress admin area.
In the next article, we’ll look at how we can add support for different screen sizes. This will allow our grid layout to be responsive.