Hello!This is more advanced but I was wondering if you could do a PHP tutorial on how to split posts into two or three columns on the home page? Kind of like:http://www.happyindulgencebooks.com/Thank you!
I’m going to do my best to answer this question, but it’s hard because I can’t give a “one solution fits all” answer. The code I provide here will have to be changed and adapted for your own theme. Everyone’s theme is slightly different, so all I can really do is give a generic response and hope that people reading this are able to change it to suit their own themes!
What we’ll be doing
What you see above in “after” is the roughest mockup ever. Sorry.
Requirements
If you’re going to attempt this, I advise the following:
- You are using a custom theme or a custom child theme. In other words, your theme won’t have an “update” that will then delete all your changes.
- You feel at least somewhat comfortable poking around theme files.
- You don’t have to KNOW PHP, but you do have to be comfortable working around it and copying/pasting lines of PHP code.
- You have FTP access to your blog so you can edit the theme files if you make a mistake and your blog becomes inaccessible because of a PHP error.
Step #1: Create column CSS
In order to do this, you need to have some CSS column classes in place. Your theme may already have these, or you may have to make them yourself. But basically what you need is a CSS class that acts as a column. Here’s one example for a two-column (so two posts side-by-side) layout:
.half { width: 50%; float: left; }
The key thing is in the width. Since I want two columns, I need my div’s width to be half of 100%, which is 50%. If you wanted three columns, you’d need to set the div to one third of 100, which would be 33.3%.
Note: you can name the div whatever you want. I chose “half” just as an example. In order to avoid conflicts, it might be better to prefix it with something, like: ng-half (I chose “ng” here for “Nose Graze”).
Next, we need a div to wrap around the columns and clear the float. For this example, I’ll use:
.ng-row { clear: both; }
Step 2: Finding your theme’s code for the homepage posts
The next step is finding the right file in your theme that outputs the code on the homepage for displaying your posts. In most themes this is the index.phpfile. However, some themes may have a different structure. For example, in the themes I code, I do have an index.php file, but in that file I tell it to include a separate file, which is where I keep the post code. I call that file loop-archive.php.
Now inside that file, you want to find a very specific block of code:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> [..] <?php endwhile; ?>
There will be a lot of code in between, but you need to find the bit starting with the
if (have_posts()) : while (have_posts()) : the_post();
portion. It may look slightly different from theme to theme, so look carefully. Then you need to find the end bit, which will be <? endwhile; ?>
. We’re going to be working in and around this snippet.
This portion of the code is called The Loop. What it does is basically cycle through your WordPress posts. Everything inside that snippet is repeated for each individual post on your homepage.
Step 3: The column code
a) The easy way (may be glitchy)
There is an “easy” way to do this. Right after this line:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
Add this code:
<div class="half">
(or whatever your class name was)
And right before this line:
<?php endwhile; ?>
Add this code:
</div>
In the end, your code should look like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> <div class="half"> [..] </div> <?php endwhile; ?>
With the rest of your theme’s code where the
[..]
bit is. You’re basically just wrapping your posts inside that column div. However, if your posts have varying heights, this could have some weird, unexpected results. There is a more complex method that is a lot more reliable…b) The more advanced way
The more advanced way looks something like this:
<?php $i = 0; ?> <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php if($i == 0) { echo '<div class="ng-row">'; } ?> <div class="half"> [..] </div> <?php $i++; if($i == 2) { $i = 0; echo '</div>'; } ?> <?php endwhile; ?> <?php if($i > 0) { echo '</div>'; } ?>
Basically what we’re doing it starting a counter inside the code. We start the count at 0, then begin cycling through the posts. The first step is: if the counter is 0, add the div class for a new row. Then, after that, we insert the div class for a column (
<div class="half">
), and make sure the rest of the post display stuff from your theme is in there, before closing the div. Then, at the end, we increase the counter by one (so it’s now 1). However, then we need to perform some checks to determine if we should close the row div. Since we want two columns, we say: if the counter is 2, reset the counter to 0, then close the row div.
Once that’s done, The Loop (cycling through the posts) ends with
<?php endwhile; ?>
But what happens if the loop ends when the counter is only at 1? We won’t have closed the row div! So, we perform one final check at the end: if the counter is greater than 0, close the row div.And that’s it!
If you’re not familiar with coding, I know this can look kind of confusing and intimidating.. especially if you’ve never seen/touched much PHP before. And since all themes have slightly different structures and layouts, it’s hard to have a “one size fits all” solution. Ultimately, you have to just try to apply the same logic to the theme that you have.
Just change the values for different numbers of columns
If you’re able to get it working with two columns, you can even try making it work with any number! You just have to change the values in the CSS (the div width) and in the PHP counter.
Comments
Post a Comment