I have installed Ambrosite Next/Previous Post Link Plus and I'm not sure how to configure it to achieve what I'm looking for.

I have a post category like below:

Parent category -> Sub category -> Post

If I use the traditional WP next_post links I can use TRUE or FALSE to cycle through either all posts on the site or the posts in the sub category.

How can I configure either Ambrosite Next/Previous Post Link Plus or the standard WP links to cycle through the posts that belong to the parent category and not just the sub category?

2 Answers 2


That should be default behavior, provided that the checkbox of the parent category was checked. From the codex: "If the post is in both the parent and subcategory, or more than one category, the next post link will lead to the next post in any of those categories."

  • This did work when the parent and sub category were selected you are right, but having the parent and sub category selected breaks our inherited background images, which have a higher priority. Commented Jan 11, 2013 at 12:37
  • 1
    There might be an alternative solution for you, I'll post it as a separate answer
    – diggy
    Commented Jan 11, 2013 at 13:24
  1. Put following function in functions.php. It adds the IDs of the (sub)categories and their parents to an array, removes duplicates and returns the array:

    function wpse80138_get_cat_ids( $post_id )
        $array = array();
        $cats = get_the_category( $post_id );
        foreach( $cats as $cat ) {
            $array[] = $cat->term_id;
            // parents
            if( $cat->category_parent ) 
                $array[] = $cat->category_parent;
            // children
            $subs = get_categories( 'child_of=' . $cat->term_id );
            foreach( $subs as $sub )
                $array[] = $sub->term_id;
        return implode( ',', array_unique( $array ) );
  2. Use the function with Ambrosite:

    $cats = wpse80138_get_cat_ids( get_the_ID() );
    next_post_link_plus( array( 'in_cats' => $cats ) );
  • hi Diggy, Thanks for the code, when used this function doesn't render anything? its being used like so: <?php $cats = wpse80138_get_cat_ids( get_the_ID() ); ?> <?php previous_post_link_plus( array( 'in_cats' => $cats ) ); ?> | <?php next_post_link_plus( array( 'in_cats' => $cats ) ); ?> any ideas? Commented Jan 14, 2013 at 5:06
  • @dodgerogers Seems like the array needs to be converted to a string. I updated the code in my answer. I also added a few lines to check for subcategories. Should work fine now!
    – diggy
    Commented Jan 14, 2013 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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