How to display different single post template based on author?

My current singlepost.php makes use of the following if conditions and renders the appropriate template.

<?php
$post = $wp_query->post;
    if ( in_category('4') || in_category('59') ) {
        include(TEMPLATEPATH . '/review-post.php');
        }
    elseif (in_category('5')) {
        include(TEMPLATEPATH . '/promo-post.php');
        }
    else {
        include(TEMPLATEPATH . '/regular-post.php');
        }
?>

I want to modify this such that /123-author.php single post template is rendered when a post is published by a particular author.

Read More

How can I modify/append to the above conditions so that the single post would look for a particular template whenever it notices a post made by 123author?

Thanks.

Related posts

Leave a Reply

2 comments

  1. I’m making a couple of assumptions about how your theme is setup:

    • You have two files:
      • index.php. This is the file that begins the execution of the loop.
      • content.php. This is the file in which your generic content template exists.
    • You are familiar with get_template_part(). If not, it’s easy to implement.

    For example purposes, I have two users in my system each of which are identified by ‘admin’ and ‘user.’

    At this point, we’ll create a second template for displaying content by a certain author. In the root of your theme’s directory, add a new template file – this file will be used to render content for a specific author.

    Assuming that your basic template file is content.php, add content-user.php. You can add whatever you want to this file – a good place to start may be copy and pasting the content from content.php. Note that the name of the new template doesn’t matter, but it’s a bit clearer since it’s going to render content by author named ‘user.’

    Next, hop into your index.php file and locate the loop. Odds are it looks something like this:

    while( have_posts() ) : the post(); 
      get_template_part( 'content', get_post_format() );
    endwhile;
    

    If your theme doesn’t support post formats and/or you’re using standard include files as you mentioned above, it may look like this:

    while( have_posts() ) : the post(); 
      include_once( 'content.php' )
    endwhile;
    

    For purposes of this answer, I’m going to go with the first example. That’s the best practice for including different template files into your content.

    At this point, we need to setup a check to include the template based on the user. I’m going to setup my loop display a template for all articles written by ‘user’ and display a separate template for all articles written by ‘admin’ and anyone else:

    while( have_posts() ) : the post(); 
      if( get_the_author() == 'user' ):
          get_template_part( 'content-user', get_post_format() );
      else:
          get_template_part( 'content', get_post_format() );
      endif;
    endwhile;
    

    The code should read easy enough, but to be clear:

    • First, we’re checking the author of the current post
    • If the author is ‘user,’ then we are using the second template we created
    • Otherwise, we’re using the default template

    Your mileage may vary based on your theme is setup but taking advantage of the get_the_author() function should help you in dynamically selecting a template.

    Update Based on Answer Comment

    You can use get_the_author_meta.

    Without seeing more of your code, I can’t be certain how your theme is setup, but if I had to guess based on the code you provided, I’d suggest trying something like this:

    $post = $wp_query->post;
    if(get_the_author_meta('user_nicename', $post->post_author) == 'user') {
        /* Do your author-specific work */
    } else {
        if ( in_category('4') || in_category('59') ) {
            include(TEMPLATEPATH . '/review-post.php');
        } elseif (in_category('5')) {
            include(TEMPLATEPATH . '/promo-post.php');
        } else {
            include(TEMPLATEPATH . '/regular-post.php');
        }
    }