In the previous post, I shared a guide on how to create a front page, register multiple widgets and display them on your front page.
While that’s still useful, there is a better, smarter and simpler approach to get it done, thanks to Sridhar Katakam.
So, let’s jump in!
Simple Way to Register Multiple Widget Areas
Let’s say you want to register two widget areas on your front page.
Here’s the simpler and smarter approach that helps you write less code but still gets things done.
Add the following code to your functions.php file
Explanation: Essentially, we are using a
for loop and incorporating an ‘incremental
php operator’, i.e,
$i++ to generate a specified number of widget areas without repeating the step over and over again. Therefore, just change the digit, i.e, 2 in the above code to generate as many widget areas as you want.
You can read more about how
php operators work here.
Simple Way to Display Multiple Widget Areas
Considering the above scenarios, let’s say we want to display the above two widget areas on our front page.
Here’s the simpler approach:
Add the following code to your front-page.php file
As you an see, we’re using a comparison operator, i.e,
<= which essentially means “Less than of equal to”. What if you want to manipulate the above code to register widget areas differently? Choose from these PHP Comparison Operators.
Note: While using this method, always be sure to double check the code for accuracy. Otherwise, it won’t work.