• I was reading a guide on how to show different single.php-files by using the original single.php only as a navigation wheel with a small code that pointed to other “single.phps” (but with new names of course) based on what category they were posted in.

    I wanted to do exactly the same based on authors instead of categories. (The reason being I want to show different ads depending on which author wrote a post).

    With categories the code in single.php would look something like this:

    <?php $post = $wp_query->post;
     if (if_category('2')) {
     include(TEMPLATEPATH . '/single_version1.php');
    } elseif (if_category('3')) {
    include(TEMPLATEPATH . '/single_version2.php');
    } elseif (if_category('4')) {
    include(TEMPLATEPATH . '/single_version3.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    Based on this code, I tried to use “if_author” but that didn’t seem to work at all. So then I found out that “is_author” was probably more correct, and yes, things looks good, but they are still not showing. Only the “single_original” after the “else”-thing is showing, on every post, no matter which author wrote it.

    Am I doing this wrong or is this not possible with authors?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Try like this:
    if ( (is_single()) and (is_author(‘4’)) ) {
    ….

    Thread Starter yp

    (@yp)

    Hi Yakuphan,

    I really appreciate the help, but could you please explain a little closer… I didn’t quite understand what the actual code should look like.

    Here’s the code that didn’t work:

    <?php $post = $wp_query->post;
     if (is_author('2')) {
     include(TEMPLATEPATH . '/single_ole.php');
    } elseif (is_author('3')) {
    include(TEMPLATEPATH . '/single_melissa.php');
    } elseif (is_author('4')) {
    include(TEMPLATEPATH . '/single_camilla.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    Could you please point me in the direction of what this should look like? I’m new to all of this.. 🙂

    I believe the idea is this:

    <?php $post = $wp_query->post;
     if (is_author('2')) {
     include(TEMPLATEPATH . '/single_ole.php');
    } elseif (is_author('3')) {
    include(TEMPLATEPATH . '/single_melissa.php');
    } elseif (is_author('4')) {
    include(TEMPLATEPATH . '/single_camilla.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    … replaced with this:

    <?php $post = $wp_query->post;
     if ( (is_single()) and (is_author('2')) ) {
     include(TEMPLATEPATH . '/single_ole.php');
    } elseif ( (is_single()) and (is_author('3')) ) {
    include(TEMPLATEPATH . '/single_melissa.php');
    } elseif ( (is_single()) and (is_author('4')) ) {
    include(TEMPLATEPATH . '/single_camilla.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    Thread Starter yp

    (@yp)

    Thank you, cais!

    I tried your version but unfortunately the result was the same as the other code.

    This really seems like it isn’t “meant to be”… if anyone has other suggestions, please let me know. Or else I’m ending up with doing the category-version, which I really hoped to avoid…

    i don’t think the conditional tags like is_author(x) are set up until you run the the_post() function. try that instead of $post = $wp_query->post;

    Thread Starter yp

    (@yp)

    Hi alanft,

    Thanks…
    Based on what you said I tried this code:

    <?php the_post();
     if ( (is_single()) and (is_author('2')) ) {
     include(TEMPLATEPATH . '/single_ole.php');
    } elseif ( (is_single()) and (is_author('3')) ) {
    include(TEMPLATEPATH . '/single_melissa.php');
    } elseif ( (is_single()) and (is_author('4')) ) {
    include(TEMPLATEPATH . '/single_camilla.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    Is this what you meant? It didn’t work though. There were no “fatal errors” but the post itself was missing. Only the bottom of the page, the footer and that stuff, were showing.

    I got the feeling I was close, if I just could get the post to show, maybe it would work? Any suggestions anyone?

    do your sub-php files like single_ole.php contain the_post()? in which case delete that.

    Thread Starter yp

    (@yp)

    Yes, it does… It’s part of a longer code:

    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?>

    Should I just remove the “the_post()” part or more?

    yes, you will need to unwrap the if/while/the_post() from those sub-php files and put that back in your master single.php

    Thread Starter yp

    (@yp)

    You mean like this?

    <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post();
     if ( (is_single()) and (is_author('2')) ) {
     include(TEMPLATEPATH . '/single_ole.php');
    } elseif ( (is_single()) and (is_author('3')) ) {
    include(TEMPLATEPATH . '/single_melissa.php');
    } elseif ( (is_single()) and (is_author('4')) ) {
    include(TEMPLATEPATH . '/single_camilla.php');
     } else {
     include(TEMPLATEPATH . '/single_original.php');
    } ?>

    It didn’t work. Only got a “Parse error”-message. And yes, I removed the same code from the other single.php-files…

    well it could be you missed out both the endwhile and endif at the other end.

    trying something custom like this needs a bit more php practice though to get it right i reckon.

    Thread Starter yp

    (@yp)

    Any other suggestions anyone? I just “know” that this can’t be impossible… After all, it’s WordPress 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Showing different single.php’s based on authors?’ is closed to new replies.