WordPress.org

Ready to get started?Download WordPress

Forums

Conditional author include on single.php (8 posts)

  1. DianeV
    Member
    Posted 7 years ago #

    I've spent many hours on this, and searched the forums and the Web for every combination of terms I can think of.

    What I'm trying to do is, on single.php, call an include for a particular author (the include contains a blurb). What I have so far is below, but I can't seem to exclude all other authors, or include only that author -- it displays for every author:

    <?php if (the_author_ID !=('2') ) { ?>
    <?php include (TEMPLATEPATH . '/joe.php'); ?>
    <?php } ?>

    I've tried it with other other tags, like the_author, but I get the same type of result. Can anyone lend a hint?

    Obviously, we could just manually add the author's blurb into her every post, but that makes it difficult to change the blurb if her info changes.

  2. jbbrwcky
    Member
    Posted 7 years ago #

    Have you tried the_author_ID() ? (the brackets are the thing)

  3. DianeV
    Member
    Posted 7 years ago #

    Thanks. Yes, I tried the_author_ID() -- which puts the author's ID number (and the joe include) after every post regardless of author.

    Same with the_author_ID(4). Also tried various author-related tags I could find. The result was that either the thing wouldn't display at all, or it displayed on every post regardless of author.

    I'm wondering if this just can't be done on single.php with author tags. Or whether it needs some sort of nested tags that are beyond what I can do.

  4. researcher
    Member
    Posted 7 years ago #

    i wondered why this is not working?
    is the is_author filter perhaps disabled?

  5. researcher
    Member
    Posted 7 years ago #

    to be shure that my storie here is true i tested this code:

    <?php if (is_single() && is_author('1')) { include (TEMPLATEPATH . '/single-author1.php'); } elseif (is_single() && is_author('2')) { include (TEMPLATEPATH . '/single-author2.php'); } else { printf(__('Sorry, this is not working.')); } ?>

    first in the index.php. that did not respond into a result i had in mind: author_1 had to show in the left column, author_2 in the right column. the ordenairy single.php shows up at the center.

    then i tried one level higher and put this code into the header.php nothing at all.

    question: why do we have a Conditional Tags page in the codex?
    or is there someone who discovered the magick of the is_author trick?

  6. natalie
    Member
    Posted 6 years ago #

    I've tried to do something similar to this, which is how I found this thread. The way I understand is_author though is that it means "if we're on an author page", then narrowing it down, "if we are on _john's_ author page". Outside of author pages, there's no way to say "if _this post_ is by john".

    The only thing I've been able to do is with CSS:

    <div class="single" id="<?php the_author_login(); ?>">

    Then, style that page differently:

    #john div.blurb { styles for john here }

    What you _could_ do maybe is put the include there but say display:none, then display:block just for john.

  7. natalie
    Member
    Posted 6 years ago #

    btw, this tip works great in the body tag as well, setting a class/id to the body based on whatever page you're on, then using CSS to display/style things at will.

    On my sites I essentially have this (but much more involved):

    <body id="<?php echo $slug; ?>" class="<?php if(is_single()) { echo 'single by-'; the_author_login(); } ?>">

    Which gives me something like this:

    <body id="post-slug" class="single by-john">

    oh yeah, slug relies on another piece of code in the <head>:

    
    <?php
        $post_id = get_post($post->ID); // get current post or page
        $slug = $post_id->post_name; // define its slug as $slug
    ?>
    

    hope that helps. I've had a number of headaches to get these things to work, but it's beautiful that WP allows us such freedom to do all of this. :)

  8. natalie
    Member
    Posted 6 years ago #

    oh! i had a hunch just now and tried something and it worked! Try this:

    <?php
    $by_john = false;
    if($post->post_author == '1') {
    $by_john = true;
    }
    ?>
    
    // then inside the loop...
    
    <?php if($by_john) { ...do stuff for john... } ?>

    Change the number 1 to john's author ID

Topic Closed

This topic has been closed to new replies.

About this Topic