Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 3 • The Loop 53


add_theme_support( 'post-formats', array(
'aside',
'gallery',
'video'
) );
}


This would add the smashingtheme_setup() function to the after_setup_theme
hook, which in turn means that it will run the add_theme_support() code for the post
formats. The ones you want to enable are passed in an array. I’ll revisit setting up theme
functions properly in the next chapter, “WordPress Theme Essentials.”


More details on post formats are available in the Codex at http://codex.wordpress.
org/Post_Formats.


THE GET_TEMPLATE_PART() AND POST FORMATS


Some theme designers prefer not to put the entire loop in loop.php, but rather use get_
template_part() for even smaller chunks of code. The Twenty Eleven theme does this
pretty well, with the default post output in content.php. What you’ll notice should you go
digging in Twenty Eleven is that it has additional files for the various kinds of post formats,
so you’ll find content-aside.php and content-video.php in there. In Twenty Eleven, the start
of the loop, the while loop covered previously, is a part of the actual template file (index.php,
category.php, and so on), but the output for the posts are in content.php. This method can
make the template files a bit easier to work with.


When it really shines, however, is when you combine get_template_part() with
get_post_format(). The latter will output the post format of the particular post. In
Twenty Eleven, you’ll find this bit of code:


get_template_part( 'content', get_post_format() );


This returns the post format to get_template_part() and appends it to content, much
like it did for loop in your previous look at get_template_part() in this chapter. So if
the post has the post format aside, get_template_part() will include content-aside.
php. This way, you can easily separate different kinds of content and how they are outputted,
which is pretty handy.


Naturally, it makes less sense with this setup if you don’t use post formats, but it is a handy
technique to keep in mind.


MODIFYING THE QUERY WITH PRE_GET_POSTS


There’s a fairly new hook called pre_get_posts that can be powerful. It makes it possible to
modify the query before it is actually run. However, using pre_get_posts can be a bit tricky
because it applies to not only the front end, but also the back end (or admin, if you will) — unless
you tell it not to.

Free download pdf