
Implementing sectional navigation in WordPress
Navigation is one area in which WordPress shows its origins especially clearly as a web log, or online journal, publishing platform. By default, posts are automatically daisy-chained together by date published.
This is fine if your site is focused on a single topic, such as local politics or your life, in which all posts should naturally be linked in chronological order (usually working backwards from most recent). If you write about a range of subjects using WordPress, however, you might have found its default navigational structure sub-optimal. I certainly have and this is how I have made WordPress work better for me.
To encourage visitors to stay on my site, as well as writing original and readable posts, I want to deliver relevant related content at the end them. It makes most sensefor links at the top and bottom of a hotel review post, for example, to refer to the previous post in my Travel section (the category to which I assigned it, in WordPress parlance), rather than the immediately previous post on the site, which is likely to be in a different section entirely.
Achieving this magazine-style link behaviour actually requires only a very minor tweak to perhaps one or two template files, instructing WordPress to link only to posts in the same category using the previous_posts_link() and next_posts_link() template tags. This is documented in the WordPress Codex and explained more helpfully in Digging into WordPress. Essentially, having found the right place in the code, all I needed to do was add ‘true’ as the final value in the tags:
<?php previous_post_link('%link', '%title', TRUE ); ?>
and
<?php next_post_link('%link', '%title', TRUE ); ?>
To be absolutely clear, a comma followed by a space, followed by TRUE and another a space must be inserted between the last value and the closing parenthesis. In the default twentyten theme, this may be found at lines 21-22 and 61-62 in the file loop-single.php.
WordPress now happily keeps visitors in the same section – some of them are, admittedly, thinly populated as yet – and will, I hope, help to keep my visitors a little more engaged with my content.
I should add that I made these changes within a child theme. This is another aspect of the WordPress architecture that is easy to implement and means that you won’t lose your changes if you download an updated version of the third party theme you are using.