We start to work on the blog area of the site. We want to have the blog area of the site at /blog/ – so we end up making a page template for that called page-blog.php and using the Settings > Reading setting to make that page the blog homepage. This way we have very specific control over that page and what it shows. In that template, we use a custom WP_Query loop to give us exactly what we want.
We decide that /blog/ should show the entirety of the most recent blog post. That makes sense for what the user has clicked from the homepage (a big button that says LATEST WRITING: Name of Blog Post). Later we’ll deal with how to handle the archives.
Speaking of the homepage, we use pretty much the same query/loop on the home.php template to get that most recent blog post name.
Aren’t you supposed to reset the query after using WP_Query()? Or does it matter if you are only using it once on a page?
I don’t think it matters. WP_Query doesn’t affect the default query on the page. It would matter if we used query_posts. Although it’s funny, in the Codex for WP_Query, their examples DO reset the query. But… they even say right before it it doesn’t matter.
At some point you said that wordpress detected the template for the blog page without having the /*Template name: Blog */ between <?php and ?> => well.. it detected it because of the name of the .php file you just created.
Wordpress detects all files page-yourcustomname.php and asigns them automatically to http://yourdomain.dev/yourcustomname (if you have already created that page in the backend)
This way you don’t have to go to that specific page and on the dashboard and selecting the template you just created. So in this case there is no need for the /* Template name: Blog */ line.
I’ve been following css-tricks from the beginning and it’s safe to say that this website was one of my trusted sources for webdevelopment. Thank you for all the great things so far!
Why was the blog even necessary when it was just showing a single post anyway?
The home page nav could just bring in the latest post and link to the permalink and just use the single.php template to display it. I’m new to WP so I’m may be missing something.
Great series though.
That’s certainly a possibility. The main difference right now is that the /blog/ page doesn’t list out all the comments. In the future /blog/ could be used to display multiple blog posts instead of just one. It turned out to be useful as it was, as a target for ajax to pull in the latest post only, but that could easily be updated.