WordPress.org

Ready to get started?Download WordPress

Forums

Showing different single.php's based on authors? (13 posts)

  1. yp
    Member
    Posted 4 years ago #

    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?

  2. yakuphan
    Member
    Posted 4 years ago #

    Try like this:
    if ( (is_single()) and (is_author('4')) ) {
    ....

  3. yp
    Member
    Posted 4 years ago #

    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.. :)

  4. Edward Caissie
    Theme Review Admin
    Posted 4 years ago #

    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');
    } ?>
  5. yp
    Member
    Posted 4 years ago #

    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...

  6. alanft
    Member
    Posted 4 years ago #

    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;

  7. yp
    Member
    Posted 4 years ago #

    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?

  8. alanft
    Member
    Posted 4 years ago #

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

  9. yp
    Member
    Posted 4 years ago #

    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?

  10. alanft
    Member
    Posted 4 years ago #

    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

  11. yp
    Member
    Posted 4 years ago #

    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...

  12. alanft
    Member
    Posted 4 years ago #

    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.

  13. yp
    Member
    Posted 4 years ago #

    Any other suggestions anyone? I just "know" that this can't be impossible... After all, it's WordPress :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.