0

Im using the next post link function of WP to show next and previous navigation links to the "next post in same category" all good with this...the issue i have is that when I'm in the "last" post of a category, the "NEXT" link appears anyway and doing what i dont want: linking to a different category 1st post. the same with the 1st post of a category, when browsing the first post of a category the "PREVIOUS" link appears linking to previous post of a another different category... I would like that in the 1st and last post, this links aren't shown.

<div id="nav-above" class="navigation">
                    <div class="nav-previous"><?php previous_post_linknext_post_link( '%link', '<span class="meta-nav">' .     
    _x( '&#9668; Previous', 'Previous post link','category' ,TRUE ) . '</span>' ); ?></div>
                    <div class="nav-next"><?php next_post_link( '%link', '<span class="meta-nav">' . _x( 'Next &#9658; ', 'Next post link', 'category',TRUE ) . '</span>' ); ?> </div>
                </div><!-- #nav-above -->

http://codex.wordpress.org/Function_Reference/next_post_link

4

You can do this by using previous_post_link() and next_post_link().

These functions will create the links for you, and you should be able to get rid of all of the logic you are using for pagination.

As you wish to only link to posts in the same category you should use the functions with the following parameters:

previous_post_link('&laquo; %link', '%title', true);
next_post_link('%link &raquo;', '%title', true);

Update

In response to your updated question with regard to your issue of previous/next linking when they are the first/last post, please see this line from the Codex of both previous_post_link() and next_post_link() in relation to the $in_same_term parameter:

Indicates whether next post must be within the same taxonomy term as the current post. If set to 'true', only posts from the current taxonomy term will be displayed. If the post is in both the parent and subcategory, or more than one term, the next post link will lead to the next post in any of those terms.

With that in mind, I suspect your first last Posts may be associated with more than one category? If that's the case the wp_get_object_terms filter may be able to help.

In your original question (pre-edit) you were only searching for posts in the very first category, so I'll apply that logic here:

<?php add_filter('wp_get_object_terms', 'my_custom_post_navigation', 4, 99); ?>
<div id="nav-above" class="navigation">
    <div class="nav-previous">
        <?php previous_post_link( '<span class="meta-nav"> %link </span>', _x( '&#9668; Previous', 'Previous post link', 'category') , TRUE ); ?>
    </div>
    <div class="nav-previous">
        <?php next_post_link( '<span class="meta-nav"> %link </span>', _x( 'Next &#9658;', 'Next post link', 'category') , TRUE ); ?>
    </div>
</div><!-- #nav-above -->
<?php remove_filter('wp_get_object_terms', 'my_custom_post_navigation', 99); ?>

In addition to the above, you should place this in your functions.php file:

/**
 * Return only the first category when outputting the previous/next post links
 */
function my_custom_post_navigation($terms, $object_ids, $taxonomies, $args){

    return array_slice($terms, 0, 1);

}
  • HI David, thanks for your response. I have made a new question better asked, i would love to hear your feedback: wordpress.stackexchange.com/questions/178800/… – dmtnexer Feb 20 '15 at 13:35
  • You shouldn't make a new question, as it is basically an extension of this. If the code I provided is not right we can discuss it here, in the comments. And if required you can edit your question to include updated information. – David Gard Feb 20 '15 at 13:38
  • updated question... thanks for clarifying on how to use this properly. – dmtnexer Feb 20 '15 at 13:40
  • deleted the duplicated post... – dmtnexer Feb 20 '15 at 13:47
  • @dmtnexer - Please see my update. – David Gard Feb 20 '15 at 13:58
1

If you need to restrict prev or next post link for specific custom taxonomy or term, custom post type, custom field or format then you should try Ambrosite Next/Previous Post Link Plus. This plugin creates two new template tags — next_post_link_plus and previous_post_link_plus — which are upgraded versions of the core WordPress next_post_link and previous_post_link template tags.

  • Sort next/previous post links on columns other than post_date (e.g. alphabetically).
  • Sort next/previous links on custom fields (both string and integer sorts are supported).
  • Full WordPress 3.3 compatibility, including support for custom post types, custom taxonomies, and post formats.
  • Loop around to the first post if there is no next post (and vice versa). Retrieve the first/last post, rather than the previous/next post (for First|Previous|Next|Last navigation links).
  • Display post thumbnails alongside the links (WordPress 2.9 or higher).
  • Truncate the link titles to any length, and display custom text in the tooltip.
  • Display the title, date, author, category, and meta value of the next/previous links.
  • Specify a custom date format for the %date variable.
  • Restrict next/previous links to same category, taxonomy, format, author, custom field value, custom post ID list, or custom category list.
  • Exclude categories, custom taxonomies, post formats, or individual post IDs.
  • Three category exclusion methods for greater control over the navigation stream.
  • Return multiple next/previous links (e.g. the next N links, in an HTML list).
  • Return the ID, title, date, href attribute, or post object of the next/previous links, instead of echoing them to the screen.
  • Return false if no next/previous link is found, so themes may conditionally display alternate text.
  • Works with Post Types Order and other popular post reordering plugins.
1

Copy the single.php page from your Parent theme and Paste it to your Child-theme's directory. Open the single.php from child-theme directory and add the following code at the end of file [before get_footer(); ]

<?php
$post_id = $post->ID; // Get current post ID
$cat = get_the_category(); 
$current_cat_id = $cat[0]->cat_ID; // Get current Category ID 

$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
$posts = get_posts($args);
// Get IDs of posts retrieved by get_posts function
$ids = array();
foreach ($posts as $thepost) {
    $ids[] = $thepost->ID;
}
// Get and Echo the Previous and Next post link within same Category
$index = array_search($post->ID, $ids);
$prev_post = $ids[$index-1];
$next_post = $ids[$index+1];
?>

<?php if (!empty($prev_post)){ ?> <a class="previous-post" rel="prev" href="<?php echo get_permalink($prev_post) ?>"> <span class="meta-icon"><i class="fa fa-angle-left fa-lg"></i></span> Previous</a> <?php } ?>

<?php if (!empty($next_post)){ ?> <a class="next-post" rel="next" href="<?php echo get_permalink($next_post) ?>">Next <span class="meta-icon"><i class="fa fa-angle-right fa-lg"></i></span> </a> <?php } ?>

After adding this code, paste the following code into your child-theme's Style.css to style the links:

a.previous-post, a.next-post {
    color: #fff;
    background-color: #4498e7;
    text-align: center;
    height: 34px;
    line-height: 34px;
    font-size: 14px;
    border: 1px solid;
    padding: 0 20px;
    margin-bottom: 30px;
    text-transform: uppercase;
    border-radius: 4px;
    font-weight: bold;
}

a.previous-post:hover, a.next-post:hover {
    color: #4498e7;
    background-color: #fff;
}

a.previous-post {
    float: left !important;
}

a.next-post {
    float: right !important;
}

Let me know the results :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.