522

professional dilettante

WordPress’ Built-in Category and Single post Navigation

For navigating to older and newer posts from a single post page, WordPress gives us some useful functions like previous_post_link() and next_post_link(). Those are easily confused with the similar looking functions, previous_posts_link() and next_posts_link(), used for navigating between pages listing multiple posts, in a category for example. WordPress gives us the category structure. We can use categories and permalinks [...]

For navigating to older and newer posts from a single post page, WordPress gives us some useful functions like previous_post_link() and next_post_link(). Those are easily confused with the similar looking functions, previous_posts_link() and next_posts_link(), used for navigating between pages listing multiple posts, in a category for example.

WordPress gives us the category structure. We can use categories and permalinks to easily organize our posts. This means we can access our posts in the fictional category named “foo” by visiting the following URL:

http://yourdomain.com/category/foo

And if the number of posts in your category exceeds the number of posts allowed per page ( specified in Settings -> Reading ), the URL becomes

http://yourdomain.com/category/foo/page/2

Navigating multiple pages, each with multiple posts

Not all of your posts will fit on a single page if you have many posts in a category and your WordPress settings limit the number of posts per page. If you have ten posts in the press category, and you’re settings are two posts per page, the posts in the press category will be spread over 5 pages. To create the navigation links between pages of posts in a category, we can use posts_nav_link(),  or for more control you can use the individual previous_posts_link() and next_posts_link(). Each of these functions takes options to customize the text of the previous and next buttons, or you can call it with no arguments. e.g.,

<?php
/* File: archive.php */
posts_nav_link();
?>

The default function with no args will produce something like:

« Previous Page        Next Page »

Navigating between single posts

On a page that shows a single post, we can create previous and next buttons to get to the next single post. The previous and next buttons in this case will lead to the user to a full post, not a page with multiple posts. We can create these buttons using previous_post_link() and next_post_link(). e.g.,

<?php
/* Files: single.php, page.php */
previous_post_link('<span class="prev_post_link">%link</span>','Previous: %title',TRUE);
next_post_link('<span class="next_post_link">%link</span>','Next: %title',TRUE);
?>

Would produce:

Next: Post Title of your Post

Paginating a Single Post or Page

Have a really long page or blog post? WordPress also lets you spread a single page or blog post over several pages. The built in function called nextpage will automatically separate your content into pages where ever you place the tag. Nextpage is a WordPress Quicktag, similar to more. Unlike the more quicktag, nextpage is not available by default in WordPress version of TinyMCE. We can easily add a button for “next page” to the WordPress editor by using a plugin. I used NextPage Buttons. To get wordpress to properly display the pages and page numbers on single blog posts or pages, you need to use the wp_link_pages() function inside the loop on page.php or single.php, e.g.,

<?php
/* Files: single.php, page.php */
wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));
?>

Leave a Reply

Required fields are marked with an asterisk (*), you may use these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free

Most Recent Post

posting data with curl from the command line

Before i forget, an easy way to test if you can connect to an external api is to use CURL on the command line. This nice as a sanity check/proof of concept to ensure you have the right token, proper field mappings, submitting the right data. This saves you from setting up a test form, [...]

Categories

Content © professional dilettante
Proudly powered by WordPress
Theme designed by Artisan Themes

Entries (RSS)
Comments (RSS)

22 queries.
0.503 seconds.