Most of the PHP code you’ll need for your WordPress install can be found in the codex. The codex is simply a huge repository of instructions, code and support. It’s a wonderful place for WordPress developers to hang out because, not only does it contain much of what you’ll need to answers questions and solve problems, it’s also a really nice learning opportunity. The more you browse, the more you begin to understand the inner workings of how much of WordPress is assembled.
While the codex is a large area with tons of code, in this post, we’re only going to concern ourselves with theme development. So, if you scroll down towards the middle of the main codex page, you’ll see a heading called, “Working With Themes.” We’ll be in there.
Inside the WordPress codex, there’s an area that lists all template tags by category. If we visit the page, we can locate the category we’re interested in and browse through each template tag that category contains. If something looks helpful, we’re able to click that tag and visit the actual tag page that contains all the information related to it.
By default, template tags do something. They either return something short and simple or return something much more complex. To take advantage of the short and simple tags is easy. You simply place the tag in your PHP code inside your template file. You may end up with a URL or a title or something as straightforward as that. Taking advantage of the more complex tags requires a bit more thought. In some cases, you’ll have to tell WordPress exactly what you’re interested in accomplishing by using what are called, “parameters.”
For instance, if we look at the “the_title” template tag, we can see that it accepts three parameters – $before, $after and $echo.
<?php the_title( $before, $after, $echo ); ?>
Each of these parameters plays a different role. For many template tags, parameters are optional. For others, they’re required. Fortunately, all optional and required parameters are listed and described on its related template tag page. In this post, I’ll go over some examples of what I’m talking about.
Conditional Statements & Loops
Conditional Statements: In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. (source)
Loops: In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached. (source)
The foundation of conditional statements (or conditionals) and loops throughout much of programming is common. And as I stated above, the concepts aren’t all too challenging to grasp. In the most basic sense, in WordPress, a conditional and loop is constructed something like this:
IF posts exist and WHILE they exist
// DISPLAY CONTENT
// DISPLAY NO CONTENT MESSAGE
All the information on WordPress loops can be found in the codex. And lucky for us, I’ve already written a fairly in-depth post that covers some of the ins and outs of the loop.
A while back, I wrote a post that covered a bit of working with WordPress templates. In that post, I talked about something called, “WP_Query.” WP_Query is the ultimate when it comes to making your WordPress loop output a certain way. It can filter which posts display on a certain page, sort them a specific way, display posts written by one or many authors and more. It really is a tool that needs to be worked through. The codex page I linked to above is a long one and gives great examples of how WP_Query can be used.
Instead of writing any code examples, I’ve decided that linking to external blog posts might be more helpful when it comes to talking about this topic. Truthfully, a conversation on WP_Query is a post unto itself and I think it’s beyond the scope of what I’m talking about here. So, if you’re interested in delving deeper into WP_Query, please take a look at the resources below:
Mastering WP_Query: An Introduction – Tuts+ Code Tutorial
I know this was only the beginning of a chat regarding the use of PHP in WordPress and that’s why I intend to continue writing on this topic in future posts. For now though, if you’ve got any comments or questions, please leave them below. Also, if you would like to browse through my articles on WordPress, please take a look at my WordPress category.