Support » Fixing WordPress » How do I customize template via “$post_author” variable???

  • I am looking for a way to insert a code into my template dependent upon which user’s post the page is on. However, this may go into the header and sidebar, not just post pages. Basically something like this:

    <?php if [THISPAGE]($post_author=”Joe”) { echo ‘<img src=”joesimage.gif”>’ };
    else { echo ‘<img src=”regularimage.gif”>’ } ; ?>

    Then it would show “joesimage.gif” (just as an example), on all of Joe’s posts. And if the page was NOT one of Joe’s posts (i.e. other people’s posts or pages like categories, archives, etc.) ALL other pages not posted by anyone (categories for example), then it would show something else.

    How would I code it so it will change that code in the header.php and sidebar.php (rather than the post.php template)? I know it’s incorrect, so if someone can just tidy it up properly so it can do what I’m asking, I’d extremely appreciate it.

    Thank you for your time.

Viewing 14 replies - 1 through 14 (of 14 total)
  • if ( is_author('Joe') ) {
    //do whatever for Joe

    See Conditional Tags

    Well that’s weird, I tried that but it didn’t work.

    <?php if (is_author(‘admin’) ) {
    echo ‘hello’;

    I put that in the template, in header.php and even in the posts themselves with single.php and “hello” didn’t show up in either. What am I doing wrong? I get the author part now I think, but can you give me the full code? Or does it not work with WordPress MU? Thanks for your time.

    Hmmm… doesn’t work for me either. I tried both within and outside the loop. I tried it with names, numbers, and empty which is supposed to check for the presence of any author:

    <?php if ( is_author()) { echo "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; } ?>

    Not sure what we’re doing wrong

    In addition to the conditional tags page here is one on author template:


    Looking in the Kubrick theme, here is a usage:

    <?php /* If this is an author archive */
    					elseif (is_author())
    					{ ?>
    						<h2 class="pagetitle">Author Archive</h2>
     	  					<?php /* If this is a paged archive */

    in archive.php… maybe you can look at that and tinker with it

    With the WordPress Default theme, in wp-content/themes/default/index.php I changed this line from:

    <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>


    <small><?php the_time('F jS, Y') ?>  by <?php the_author_posts_link() ?> </small>

    Then in wp-content/themes/default/archive.php, just before:
    <?php while (have_posts()) : the_post(); ?>
    I added this:

    if ( is_author('admin') ) {
    echo 'this is the admin author calling!!!';

    and it did display “this is the admin author calling!!!” (along with the posts for that author) when I clicked on the “admin” author name displayed on a post on the blog’s main page.

    Are you sure you are using that code in an Author Template?

    —- Edit —-
    ^^^ Thanks, Michael, that’s helpful.

    I don’t think I quite understand what the tags are doing.

    <?php if ( the_author()) { echo "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; } ?>

    Does display on pages with an author while is_author does not…

    I don’t think you are using the Author Template properly. Please follow the steps I described with the WordPress Default theme. Also review Template Hierarchy to understand how WordPress processes theme templates.

    If your question is about WordPress MU, please address that question to

    Also note, the_author() is meant to be used in a loop, where is_author() is meant to test if the page (not to be confused with Page) you are viewing is an author archive.

    NO, NO, NO, you misunderstand!!! Didn’t you read what my goal is??

    I don’t want it to just display their name (as “the_author”) does. I don’t want it to “check” if there IS an author.

    What I want is for it to check WHO IS the author, and if it is Joe, for example, then it would display “Joe’s image” in the header. If it is Sally, then Sally’s image. Simple enough.

    <?php if ($post_author = ‘admin’) {
    echo ‘joes image here’;

    Problem is it doesn’t appear at all in the header.php with that code, and if it’s on the single.php it always appears regardless of the author. I just want to fix that please.

    Why doesn’t that code work? What code would work?

    I keep trying variations and different things with no success. All I want is to be able to have a string check who is the author of a post, and if it is Joe, then display Joe’s image on all of Joe’s posts, and if it is Sally, Sally’s image on all of Sally’s posts.

    Is this impossible with wordpress? Why? Is there a way to create a variable specific to each author? That would allow me to accomplish this.

    $author = get_query_var('author');
    echo 'this is the id for the queried author (use in an archive or author template): '.$author;

    So how would I use that?

    If I put, for example:

    $author = get_query_var(‘author’);
    if ($author = ‘admin’) {
    echo ‘hello’;

    It says hello on all posts not just the admin’s. How can I make it specifically show that hello (or whatever code I want) on ONLY those posts done by a specific author?

    Put that in an archive based template. Using the WordPress Default theme, put that before the loop in wp-content/themes/default/archive.php. !!!!!!!!!!!THIS IS NOT MEANT TO WORK IN A LOOP!!!!!!!!!!.

    also try
    if ('admin' == $author = 'admin') {

    Please review:
    Stepping Into Template Tags
    Stepping Into Templates
    Template Hierarchy
    The Loop
    Author Templates

    Oops, must use the author ID not the author name.

    $author = get_query_var(‘author’);
    if (1==$author ) {
    echo ‘hello’;

    So if you want this in the post loop it would be

    <?php if (1==$post->post_author) {
    echo ‘joes image here’;

    Assumes that admin is user id 1

    Hi Michael,

    I think I’m trying to do the same thing the OP was looking for, but to no avail… I’m trying to add a conditional statement to header.php that tests if the post is written by a particular author. This would only apply to actual post pages (that use single.php) and not to the homepage (index.php) or archives (archive.php). The code would then call a specific CSS stylesheet based on the author’s name:

    <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/style
      $author = get_the_author();
      if ($author=="John") {
        echo "-john";
      } elseif ($author=="Jake") {
        echo "-jake";
    .css" type="text/css" media="screen" />

    If the author is John, the stylesheet that would be called would be ‘style-john.css’, ‘style-jake.css’ for Jake, and ‘style.css’ if there’s no author (for index.php and archive.php).

    I haven’t been able to get this to work, in the <head> section of header.php. BTW, I’m testing this all out on the default Kubrick template.

    I’ve been able to get WordPress to echo the name of the author at the very top of the page using this:

      $author = get_the_author();
      echo $author;

    When viewing a single post, it will show the author’s name right at the top of the page before the header. If you’re on the homepage, nothing displays, which is exactly how I want it.

    If I drop this code into header.php anywhere before the opening <body> tag, nothing displays at all.

    I’ve tried all of this with the code you suggested, but I get the same thing.

    Any tips? Is what I want to do even possible? Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘How do I customize template via “$post_author” variable???’ is closed to new replies.