Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

  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 ) );
    
share|improve this answer
    
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? –  dodgerogers Jan 14 '13 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 Jan 14 '13 at 10:46

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."

share|improve this answer
    
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. –  dodgerogers Jan 11 '13 at 12:37
1  
There might be an alternative solution for you, I'll post it as a separate answer –  diggy Jan 11 '13 at 13:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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