Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Skippy/Advanced

When creating my theme, I had need to access things outside The Loop, but the Template Tags provided only worked inside the loop. Here's the code to my sidebar, to show how I did it:

<!-- begin sidebar -->
<div id="sidebar">
<ul>
        <?php wp_list_pages('title_li='); ?>
</ul>

<?php
// let's generate info appropriate to the page being displayed
// first, are we looking at a specific page?
if (is_home()) {
        // home page, so let's pimp Firefox
        $text = "<a href='http://www.getfirefox.com/'><img src='http://www.spreadfirefox.com/community/images/affiliates/Buttons/110x32/get.gif' alt='Get Firefox!'/></a>";
        echo "<p>$text</p>";
} elseif (is_single()) {
        // we're looking at a single post
        // get the current post's ID using the global $wp_query variable
        $ID = $wp_query->post->ID;
        // then get the permalink
        $permalink = get_permalink($ID);
        // now construct a list of categories, punctuating as necessary
        $cats = get_the_category($ID);
        $count_cats = count($cats);
        $foo = 1;
        foreach ($cats as $cat) {
                if ( ($foo == $count_cats) && ( '' != $categories) ) {
                        $categories .= " and <a href='" . get_category_link($cat->category_id) . "'>" . $cat->category_nicename . "</a>";
                } elseif ( ('' != $categories) || ($count_cats > 1) ) {
                        $categories .= " <a href='" . get_category_link($cat->category_id) . "'>" . $cat->category_nicename . "</a>, ";
                } else {
                        $categories .= " <a href='" . get_category_link($cat->category_id) . "'>" . $cat->category_nicename . "</a> ";
                }
                $foo++;
        }
        $category_list = "This post is assigned to the" . $categories;
        if (count($cats) > 1) {
                $category_list .= " categories.";
        } else {
                $category_list .= " category.";
        }
        $technorati = "<br /><a href='http://www.technorati.com/cosmos/search.html?url=" . $permalink . "' title='Technorati Cosmos for this post'>Technorati Results</a>";
        $text = $category_list . "<br />" . $technorati;
        echo "<p>$text</p>";
} elseif (is_archive()) {
        // we're looking at an archive page, so present a list of all categories
        echo "<h3>Categories</h3>";
        echo "<ul>";
        wp_list_cats('optionall=0&sort_column=name&list=1&hierarchical=1');
        echo "</ul>";
} elseif (is_page()) {
        // we're looking at a Page.  Which one?
        if ('About' == single_post_title('', FALSE))  {
                // My about page
                $text = "I've had skippy.net since 1999.  It originally started out as a collection of static HTML files, until I discovered server-side includes.  I dabbled next with phpBB for a bit before I finally settled upon WordPress.<br /><br />I keep hoping that Paraguay will let me register <code>http://skip.py</code> but that hasn't happened yet.";
                echo "<p>$text</p>";
        } elseif ('Colophon' == single_post_title('', FALSE)) {
                // My colophon.  Let's grab some quick stats: number of posts + comments
                $post_count = $wpdb->get_var("SELECT COUNT(ID) FROM " . $wpdb->posts . " WHERE post_status = 'publish'");
                $comments_count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM " . $wpdb->comments . " WHERE comment_type = ''");
                $text = "There are currently $post_count posts, and $comments_count comments in the database, plus countless drafts and works-in-progress.<br />Spam Karma has eaten " . spamk_stats(TRUE, TRUE) . " spam comments.";
                echo "<p>$text</p>";
        } elseif ('Contact' == single_post_title('', FALSE))  {
                // My Contact page.
                $text = "Does my site stink?  Is it great?  Feel free to share your thoughts -- I enjoy receiving email from people.  I try to answer all the mail I get, but it sometimes takes me a little while.<br />";
                echo "<p>$text</p>";
        } elseif ('Links' == single_post_title('', FALSE))  {
                // My links page.
                $text = "What would a blog be without links to other blogs?  Most of my immediate family have blogs, as do many of my friends.";
                echo "<p>$text</p>";
        } elseif ('Syndicate' == single_post_title('', FALSE))  {
                // my syndication page
                $text = "Syndication is a great way to keep up with blogs.  If you have an aggregator, use either of the links to the right. To learn more about content syndication, <a href='http://www.newsforge.com/article.pl?sid=04/08/19/1420241'>click here</a>.<br /><br />If you don't have an aggregator, feel free to subscribe to email notifications.";
                echo "<p>$text</p>";
        }
} else {
        // failsafe, fall back to pimping firefox
        $text = "<a href='http://www.getfirefox.com/'><img src='http://www.spreadfirefox.com/community/images/affiliates/Buttons/110x32/get.gif' alt='Get Firefox!'/></a>";
        echo "<p>$text</p>";
}
?>
<h3>Worth Visiting:</h3>
<ul id="elsewhere">
<?php get_links(5, '<li>', '</li>', ' ', FALSE, 'updated', FALSE, FALSE, 3, FALSE, TRUE); ?>
</ul>

<script type="text/javascript" src="http://www.flickr.com/badge_code.gne?nsid=42774892@N00&count=3&display=random&name=1&size=square&raw=1"></script>
<br />

<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<input type="text" name="s" id="s" size="15" />
<input type="submit" value="<?php _e('Search'); ?>" />
</div>
</form>

</div>
<!-- end sidebar -->

As you can see, I'm presenting custom content based on which aspect of my page is being viewed. Some of the content is static (hard-coded into the sidebar), but it could easily be made dynamic (stored in the DB, or elsewhere). I'm also using the Links Manager to provide what my own version of the popular "asides" functionality.

Each section inside the if block ends with an echo statement to spit out the contents I generate. I would have preferred to just generate the content inside the if block, and then spit it out at the end, otuside of the if; but the function wp_list_cats() echoes directly to the user. I didn't look very hard for a non-echoing version of this function, so perhaps the above could be tidied up a bit.