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.

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.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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');
    }
} 
share|improve this answer
    
Thanks a ton for the detailed answer. However, it kind of complicates my theme's set up. Isn't it possible to add an IF ELSE condition right above the existing if-else's where I can put a check for the author id or author nicename? So if the post is not made by the author id, then switch back to the rest of the if else conditions. –  Sledge81 Feb 23 '12 at 15:37
    
See updated answer. –  Tom Feb 23 '12 at 16:36
    
WOW!!! IT WORKS. Brilliant... Thank you so much. –  Sledge81 Feb 23 '12 at 17:50
add comment

Sometime we needed to show our details page (that means single.php) in different styles. For this sense, I write a post on http://www.webinbangla.com/2012/05/different-style-of-single-php/

share|improve this answer
    
It's a short code, would be nice if you shared/pasted it here too :) –  brasofilo Oct 20 '12 at 3:05
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.