We continue porting code over from our static mockup to our WordPress index.php page. We tweak “the loop” a bit to output just what we want.
We get down far enough on the page where WordPress functions like
get_sidebar() show up. Predictably, it behaves a lot like
get_header() works. It goes and gets the
sidebar.php file and plops it’s content into the page right where that function is.
We delete quite a bit of code that was us simulating markup that we get from elsewhere and replace it with functions that end up outputing the same markup. For instance, a whole bunch of markup that made the poll area goes away and we put in wp_polls() which is a function that spits out all the stuff needed to show the poll in our actual WordPress template.
Before getting too excited and looking at our work, we move onto the footer.php file and clean that up. The big important task here is that we need to close any elements that we opened in the header.php file but didn’t close. In our case, it’s just one, the opening tag for
<div class="page-wrap"> needs to be closed.
We link up the right scripts in the footer, delete a bunch of v9 specific stuff, and tie up loose ends.
When we do go look at the page, it turns out there are a few display weirdnesses that we have to deal with. We spend the whole end of the video debugging these issues, which turn out to be new places we need to be careful about clearing floats and closing tags.
What is the call called that you use to display items or an item after a certain number of posts?
There is no specific “call.” There is a thing in WordPress called “The Loop.” It’s literally a PHP
forloop. I set a variable called
$ito 1 and increment it up by 1 each iteration of the loop. Then also during the loop I check if
$i == 2output those ads if it’s true. Nothing fancy, just logic.
Ok thanks, I was just wondering if there was a snippet in Digging Into WP for it
Just a quick thought about the WordPress individual style includes (as I noticed them in this video), the more accepted way of doing per page include is to do them in the functions.php using the wp_enqueue_style, which inserts the stylesheets in the
wp_head();hook you insert.
So then an example would be:
I could see how that would be nice.
Essentially then you are keeping logic out of the views which is something that I actually feel pretty strongly about in general. When I’m working on a Rails app I absolutely hate it when logic seeps into the views, yet that’s what’s going on in our header.php file here. I guess I’m just less sensitive to it in WordPress since we’ll probably never get away from, for example, having the Loop in views which is another kind of logic.
Another thing to consider for your home page is moving away from
query_poststo using new instances of
WP_Query. The current conventional wisdom is that you should never use
query_postsas it runs an additional SQL query, making your site less efficient. I’ll leave it up to others to do a better job of explaining why:
Andrew Nacin’s “You Don’t Know Query” Slides
When to use WP-Query, query_posts and pre_get_posts?