Putting It All Together

Before we move onto some more sophisticated modifications of our theme, I want to show you just one example of how all those template files and hooks work together to create the markup that you see when you load the site in your browser. single.php is a good template to start with; it's the one that controls the display of a single post. Pop it open with your favorite text editor and take a peek:


// calling the header.php get_header();

// action hook for placing content above #container thematic_abovecontainer();

<div id="container"> <?php thematic_abovecontent(); ?> <div id="content"> <?php the_post();

// create the navigation above the content thematic_navigation_above();

// calling the widget area 'single-top' get_sidebar('single-top');

// action hook creating the single post thematic_singlepost();

// calling the widget area 'single-insert' get_sidebar('single-insert');

// create the navigation below the content thematic_navigation_below();

// calling the comments template thematic_comments_template();

// calling the widget area 'single-bottom' get_sidebar('single-bottom');

</div><!-- #content --> <?php thematic_belowcontent(); ?> </div><!-- #container --> <?php

// action hook for placing content below #container thematic_belowcontainer();

// calling the standard sidebar thematic_sidebar();

// calling footer.php get_footer();

As we saw when we looked at home.php and footer.php, there's little in the way of actual markup in this file. If we want to find out how the post's heading, byline, and body are put together, we're going to have to dig a little deeper.

It's a Jungle in There

Thematic's extension files are well-commented, but they're often quite long. You'll soon be making fast friends with the search function in your text editor. If it features a project-wide search function that lets you search for a string across multiple files in your working directory, it's definitely a good idea to become acquainted with it now.

If your favorite editor lacks this functionality, one alternative is to search Thematic's source code on Google Code,12 where it's hosted. Just click on the Source tab, and then search for the function or hook that you're looking for. Google Code will show you every occurrence of that text across Thematic's entire codebase, complete with context and line numbers.

12 http://code.google.com/p/thematic/

thematic_singlepost is the function that pulls in the post itself. Searching for "thematic_singlepost" across the entire thematic directory reveals that the function declaration lies in library/extensions/content-extensions.php:

thematic/library/extensions/content-extensions.php (excerpt)

// Located in single.php // The Post function thematic_singlepost() {

do_action('thematic_singlepost'); } //end thematic_singlepost do_action is a way to tell WordPress that an action is taking place, so it should execute any functions that are hooked to that action. This is an important concept that might be a little confusing, so let's break it down:

1. When WordPress is rendering the single.php template, it comes across a call to the thematic_singlepost() function.

2. When it runs that function, Thematic tells WordPress to trigger the thematic_singlepost action.

3. At that point, any functions that have been attached to that hook—via add_action as we saw above—will be executed.

So, in order to find out what exactly Thematic does next, we just need to find the functions that are hooked to that action. Let's search for the next instance of the string "thematic_singlepost," which turns out to be further down in the same file:

thematic/library/extensions/content-extensions.php (excerpt) add_action('thematic_singlepost', 'thematic_single_post');

Aha! There's our add_action call. It's telling WordPress that when the event thematic_singlepost occurs, we want to do whatever's contained in the function thematic_single_post (note the extra underscore). We're getting warmer! Lucky for us, that function is located just above the add_action call:

thematic/library/extensions/content-extensions.php (excerpt)

function thematic_single_post() { ?>

<div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>"> <?php thematic_postheader(); ?> <div class="entry-content"> <?php thematic_content(); ?>

<?php wp_link_pages('before=<div class="page-link">' . ^_('Pages:', 'thematic') . '&after=</div>') ?>

<?php thematic_postfooter(); ?> </div><!-- .post -->

Found it! Well, mostly—much of this code is still calling on other functions we've yet to discover. Some of them, like thematic_content, are other functions in Thematic's library. Others, like wp_link_pages, are built-in WordPress functionality. Despite these other function calls, however, this code bears a resemblance to what we think a post should be made of. There's a div with some classes and an id, a post header function, another div with the content inside, pagination, and a post footer function.

That sure is a lot of searching to find out how one little object works. Why, you might ask, is everything stashed away in function upon function upon function? While it might seem like a lot of fiddling about, it's actually for your benefit—breaking up all the functional elements into tiny chunks means that we can target specific parts of the template, reuse a lot of those Thematic functions in our own work, and generally have a lot of flexibility to change stuff.

Was this article helpful?

+1 0


  • Roosevelt
    What is thematic_singlepost()?
    8 years ago

Post a comment