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.

Let three categories and their IDs:

cat1 = 1 (parent)
cat2 = 2 (child of cat1, parent of cat3)
cat3 = 3 (child of cat2)

Let each category has 3 posts, they are: post1, post2, post3.

And each posts are written by a specific author whose name is "author" and id is "99".

How can I list posts as sub-items of each category? It should look like:

Author Name

  • cat1
    • Post1
    • Post2
    • Post3
  • cat2
    • Post1
    • Post2
    • Post3
  • cat3
    • Post1
    • Post2
    • Post3
share|improve this question
    
Have you tried to code anything yourself? –  Brian Fegter Sep 23 '12 at 19:17
    
These Post1, Post2 and Post3 are 3 posts that are assigned to all 3 categories? How many posts do you want to show under each of categories? All of them? What if any post is assigned to multiple categories? Should it be shown only once or multiple times on this list? –  Krzysiek Dróżdż Jun 21 '13 at 20:09
    
Hope the following will help you, check this related post : stackoverflow.com/questions/10657309/… –  jothikannan Jul 1 '13 at 8:16
add comment

1 Answer

We have also shown you how to display related posts with a WordPress plugin YARPP which has its own formula of determining which posts is related or not. Some of our users asked us if it was possible to display related posts by same author which we think is a pretty handy feature for multi-author blogs. So in this article, we will show you how to display related posts by the same author in WordPress without a plugin. First, open your theme’s functions.php file and add the following code:

function get_related_author_posts() {

    global $authordata, $post;

    $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

    $output = '<ul>';

    foreach ( $authors_posts as $authors_post ) {

        $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';

    }

    $output .= '</ul>';

    return $output;
}

Then you need to open your single.php file (for twenty ten theme, loop-single.php), and paste the following code inside the loop where you like:

<?php echo get_related_author_posts(); ?>

The code above will basically display 5 recent posts by the same author, and it also make sure that there are no duplicates (i.e the current post will not be in the list). This is a very simple trick that does the trick without any hassles. You can further customize the display by adding post thumbnails or other styling by editing the output lines of the function

share|improve this answer
    
Well, it's not exactly answer to this question, I guess. –  Krzysiek Dróżdż Jun 21 '13 at 20:07
add comment

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.