Page Sensitive Multi-Level Navigation

While most sites don’t need incredibly deep page navigation there are situations that justify a hierarchy beyond the typical 2 – 3 levels.  Unfortunately that can be cumbersome for top navigation drop-downs (more than 1 level of drop down is too much IMHO) so another solution needs to be found.  I ran into just such a situation for a client and while I”m also not a fan of left hand navigation it was the decision of the client to utilize it in conjunction with their top navigation, and in retrospect it made sense for them. To keep things easily navigable we also implemented breadcrumbs (which is a good practice anyway).

The mission was to display sub-pages of the current page you are on in the left nav and once you hit the bottom of the hierarchy to show pages which are parallel to that page within the same branch of the hierarchy.

After some digging and experimentation I came up with the following which executes perfectly in only a few lines of code.

<?php
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0&depth=1");
if ($children == "")
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0&depth=1");
?>
<ul>
<?php echo $children; ?>
</ul>
<?php endif; ?>

Of course you style to taste…

That’s it! Used in conjunction with a standard WordPress top-navigation and breadcrumbs you can easily display page sensitive multi-level navigation for your super-complex multi-level site!

Tweet This | Digg This | Stumble it |

Separating Trackbacks from Comments in WordPress 2.7+

Back when WordPress 2.7 was released, the WordPress team introduced a completely revamped comment form that included integration of threaded comments into the core software, introducing some dramatic changes with how comments are handled.   Unfortunately, this change broke one of the most popular comment hacks, separating trackbacks from comments.

Since then, several people have stepped up and shared some great hacks for separating trackbacks from comment in WordPress 2.7 or newer blogs .  So far the best guide I’ve found came from Sivel.net, which can be viewed here.  Click over and follow those steps get everything separated.

Note: The above guide is only for people using WordPress 2.7 or newer installations.  For people using WordPress 2.6 or earlier, you’ll want to use this tutorial.

Once you’ve got the comments successfully separated from the trackbacks, there are a couple additional tweaks you may want to do to clean up how things look (it really depends on preference I suppose).   The first is to clean up your trackbacks/pingbacks by only displaying the title instead of an excerpt and everything else.   In order to do this, you’ll need to find the following code in your comments.php file:

<ol>
<?php wp_list_comments('type=pings'); ?>

Now replace that code with the following:

<ol>
<?php wp_list_comments('type=pings&callback=list_pings'); ?>

Lastly, you’ll need to add the following code to your functions.php file (which can be created if you don’t already have one):

<?php
function list_pings($comment, $args, $depth) {
$GLOBALS['comment'] = $comment;
?>
<li id="comment-<?php comment_ID(); ?>"><?php comment_author_link(); ?>
<?php } ?>

That should clean up the trackbacks/pingbacks section and you can also apply the same changes if you use a plugin to display tweetbacks.

The other thing you may want to do is fix the comment count to only show actual comments, filtering out the trackbacks/pingbacks which are included in your comment count by default.   Simply add the following code to your functions.php file (which again can be created if you don’t already have one):

<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}
?>

So there you go.  Anyone have any other tips for cleaning up your comment form?

Tweet This | Digg This | Stumble it |

How to: Use Thumbnails Generated by WordPress

One of ten brilliant tips that I shared yesterday on my blog – display images on your blog’s homepage without any custom fields or any additional functions.php script, something I first saw on  WebDeveloperPlus.

How do you do it? First log in, on the sidebar select ‘Media’ (which is under ‘Settings’). You’ll then be taken to a page with an option to change the thumbnail size of images. Change that to whatever size you want your images to appear as. Next, insert the code below onto your homepage, archive page, whatever.

<?php
//Get images attached to the post
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'order' => 'ASC',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$img = wp_get_attachment_thumb_url( $attachment->ID );
break; }
//Display image
} ?>

Then, to display your image you can just echo out the $img tag we just created:

<img src="<?php echo $img; ?>" alt=" " />

And there we have it. I told you it was easy! This is one of the tips from a post I wrote yesterday on WPShout – ‘10 Tips to Improve Your WordPress Theme‘.

Tweet This | Digg This | Stumble it |

How Long Should You Wait to Upgrade WordPress?

With the recent release of WordPress 2.8 this past week, there has been a number of complications and/or frustrations from the WordPress community, leading to a lot of discussion about how long you should wait to upgrade WordPress when a new branch is released.  Historically the WordPress team has always done a great job of testing their releases, which I think lead to a strong confidence from the WordPress community when it was time to upgrade.   Combine that with the one-click upgrade option that is now built into WordPress and the annoying tag reminding you to upgrade, and you’ve got a huge number of people who upgraded to WordPress 2.8 immediately upon its release.

Unfortunately, with each new WordPress branch comes changes which sometimes break WordPress plugins, create problems with the WordPress theme you are using, and usually includes changes to the code.   If you upgrade before the themes or plugins you rely on have been updated, this can cause problems.   The iThemes team recently touched on this subject with their post, When Should I Upgrade WordPress?  Their post also includes five helpful things that need done BEFORE you do your one-click upgrade:

  1. Make a backup of all your site data
  2. Upgrade of all your plugins
  3. Visit plugin and theme author websites
  4. Disable all plugins
  5. Ask yourself if you need to upgrade now

I also recommend waiting a week or so to view feedback before upgrading.

I know several of you haven’t upgraded WordPress to 2.8 yet.   How long do you plan on waiting until you upgrade your WordPress installation?  Please include which version of WordPress you are currently using with your comment!

Tweet This | Digg This | Stumble it |

How To: Hack WordPress Theme Template Pages

The key to being able to display exactly what you want in Wordpress is understanding Wordpress theme template pages. These are the theme files that display pages, not the ones that perform functions like comments, sidebar, etc. Most of us don’t use the Wordpress default theme that comes with installation, and end up downloading a free theme from the Internet. This is a great way to customize your blog, but not all theme authors code their theme the same way. The capabilities of that theme largely depend on how much time the web designer took to code it, in addition to their knowledge of Wordpress itself.

I’m going to explain everything you need to know to be able to customize all your theme pages any way you want, and this will give you enough information to begin coding your own theme as well. Even if you’re an ‘expert’ theme coder, you should learn something new from this article.

How Wordpress Works

The most important thing you could learn about Wordpress is the Template Hierarchy, or – “the order in which Wordpress calls pages”. The ONLY file that is required in the PHP files of any Wordpress theme is the “index.php”. That’s it! That one file could handle every single function Wordpress performs (if you wanted it to). Or, you could have a Wordpress theme that had a PHP theme for for every single WP function (or anything in between).

The Order of Things

Every time a Wordpress page is called the WP ‘engine’, if you will, determines (through process of elimination) what kind of page it is. It’s kind of like a “where am I?” function. Wordpress says “what page am I…” and in turn tries to call pages in a specific order. If WP doesn’t find the PHP file it needs it just defaults to the “index.php” file and uses it instead. There are 9 basic kinds of pages Wordpress looks for first:

Am I the Home Page?
If WP thinks it’s on the home page it will look for “home.php” first, and “index.php” second.

Am I Post Page?
(Single) post pages look for “single.php” first, and then default to “index.php”.

Am I a ‘Paged’ Page?
(Static) or ‘paged’ pages in Wordpress look for a “pagetemplate.php” first (if assigned when published), “page.php” second, and default to “index.php” last.

Am I a Category Page?
When Wordpress determines it’s on a category page first it looks like a category specific ID page, such as “category-7.php”. If it doesn’t find that it next looks for a “category.php” (which would be used on every category page). If that’s not there is searches for “archive.php”, and last it defaults to “index.php”.

Am I a Tag Page?
If Wordpress is on a tag page it tries to load “tag-slug.php” first, with ’slug’ being the name of your tag. If your tag is ‘wordpress hacks’ the tag slug page would be “tag-wordpress-hacks.php”. It that’s not available, WP next looks for “tag.php” which would load for all tag pages, then “archive.php”, and if that’s not there last it defaults to “index.php”.

Am I an Author Page?
If your blog has multiple authors, first it looks for “author.php” to display the details. If that’s not there, it tries to load “archive.php”, and last it defaults to “index.php”.

Am I an Archive Page?
Archive pages are loaded when Wordpress loads a date based page for previous posts. First it tries to load “date.php”, then “archive.php”, and last it defaults to “index.php”.

Am I a Search or 404 Page?
If WP determines it’s on a search (results) or 404 (not found) page the it tries to load either search.php or 404.php. If not, the default is once again “index.php”.

Am I an Attachment?
Out of all the Wordpress theme template pages, the attachment page is probably the one used least, and I have to admit – I’ve not seen a single one of these in any of the hundreds of themes I’ve downloaded. Wordpress uses these special pages usually for uploaded content, which would explain why it first looks for “image.php”, “audio.php”, “video.php”, or “application.php”. Then it tries to find “attachment.php” or “single.php”, and if none of those are available it also defaults to “index.php”.

Inner Workings of WP Theme Templates

As I said before, you could use a single index.php file to handle the 9 types of pages. You would simply code in some conditional tags, like I showed you in the last tutorial I wrote here on WP Hacks. A single index.php would then just contain code to say if is_home, do this, if is_single do that, etc. That’s a lot of code for just one page, and a bit unorganized – and it doesn’t leave a lot of room for customization.

Coincidentally, like Wordpress searches for 9 basic pages – each theme template page also contains 9 basic Wordpress elements:

  1. a header call
  2. opening of ‘the loop’
  3. a call to get the permalink and (some) meta
  4. a call telling Wordpress what to get
  5. a call to get either the content or an excerpt
  6. (maybe) more meta
  7. closing of ‘the loop’
  8. a sidebar call
  9. a footer call

Those are only the Wordpress elements, of course the PHP code to make them work is usually scattered throughout the appropriate HTML code make your theme’s layout and graphic design work properly. I’m going to explain these elements a bit more so you can understand how you can customize (or create) nearly any theme template page.

Header, Sidebar, and Footer calls

I’m going to handle all 3 of these elements at once, since they are all basically the same. When you see this code in a template:

<?php get_header(); ?>

Wordpress is simply opening the “header.php” file. The same is true for get_sidebar (sidebar.php) and get_footer (footer.php). You could have multiple headers, footers, or sidebars, see the earlier link above for conditional tags.

Opening of “the loop”

The infamous “Wordpress Loop” is when a call goes out to the database to do something until Wordpress says “stop”, i.e. ‘get me the most recent full text posts in their entirety’. The structure of ‘the loop’ changes depending on what kind of page your displaying, and each of the 9 basic types of pages Wordpress tries to load has a ‘loop’.

The opening of the loop generally looks like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

You may see it broken down with have_posts on one line to define conditional tags with the while and the_post on another, but it’s still the opening of the loop, and it’s pretty much the same in all pages. One way to use the multi-line loop opending is to place a parameter between “if have_posts” and the rest by using query_posts in between to show only a single post, posts from a time period, the last post only, posts from certain categories, or even change the ordering of posts being iterated in the loop.

A Call to Get the Permalink and (some) meta
The very last section of the loop opening (the_post) actually makes individual data available through each iteration of the loop. This data referred to usually as “post meta” because it’s descriptors and identifiers for the individual content being looped through. Typically things like the permalink (URL), title, date, etc. I say ’some’ meta, because most themes show some things before the individual post content, and then some after – such as categories and tags.

Here’s a short list of things you can call in post meta: the_permalink, the_ID, the_title, the_time, the_author, the_author_email, the_author_posts_link, the_category, single_cat_title, the_tags, single_tag_titls, edit_post_link, comments_popup_link, comments_rss_link

Example code you might see for post meta would be something like this:

<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>
</div>

A Call Telling WP What to Get
Next Wordpress will decide how much of the actual individual post content to get for you. How much is gathered from the database depends on whether your look uses “the_content” (to get it all) or “the_excerpt” (to get part of it).

(Maybe) more meta
As I previously mentioned, the common things to see after a post are assigned categories or tags, and sometimes you see an “edit” link here as well. Some themes even put date published meta after the post content.

Closing of ‘the loop’

The code looks like this:

<?php else : ?>
<?php endif; ?>

Typically it’s on more than one line in case you want to build an option in, such as a message “Sorry, we didn’t find anything”. After the sidebar, before the sidebar and footer calls, is where you typically find the “next” and “previous” navigation links.

Bastardized Loops?

Well, just because most loops look like the examples I just gave you, doesn’t mean you can’t bastardize them in just about any way you can imagine. I recommend you read the WP Codex page The Loop in Action for examples of archive, category, and single post formats – as well as static home page.

The Codex official page for the loop has several examples of how to place multiple loops in one page.

Perishable Press has a great tutorial for multiple loops, multiple columns – if you want to try and split your content up. They also have some great loop templates, in addition to a great tutorial of horizontally sequenced posts in two columns.

Conclusion

Armed with just a tiny bit of knowledge, you can hack just about any Wordpress theme template page to do just about whatever you want! Now that you understand (in great detail) how Wordpress calls it’s pages and how the loop works, you can conquer any task! Have fun customizing your blog’s theme!

This post was submitted by John Pratt, who writes many Blogging Tips, and how to create a Wordpress affiliate store.

Tweet This | Digg This | Stumble it |