• I’m setting up my blog so that an author-specific image will appear in each author’s post. I figured I would have to use an if-then statement to do it, but most of the coding I’ve tried so far just displays the author ID instead of using it as the terms of a condition. I’ve tried two different types of coding so far (I replaced the img coding for just the text “img” in this example to make it easier to read):

    <?php
    $atr=the_author_ID();
    if ($atr==1)
    echo “img1”;
    elseif ($atr==4)
    echo “img2”;
    ?>

    (the above only posts the ID numbers)

    and

    <?php
    if (the_author_ID(‘1’))
    echo “img1”;
    elseif (the_author_ID(‘4’))
    echo “img2”;
    ?>

    (the above posts the ID numbers twice, like 11 or 44)

    Help? Any advice would be really appreciated! 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Use the first example with get_the_author_ID instead of the_author_ID.

    Thread Starter sirenique

    (@sirenique)

    Otto, thanks. I switched it, but now I’m not seeing any echo onto the page itself…

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    But, I thought that was what you wanted to do? It’s what you *said* you wanted to do.

    get_the_author_ID returns the author id.

    the_author_ID outputs the author id to the page.

    Use the right one for the right task.

    Thread Starter sirenique

    (@sirenique)

    I’m sorry, I guess I didn’t phrase what I wanted properly. I wanted to generate an image based on what the author of the post’s ID number is. So if the_author_ID for a post is 4, img4 will generate in that post. If the_author_ID for another post is 2, img2 will generate in that post.

    I presumed an if-else would be the best way to go about this — I want the image to display, which is why I put it in the echo, not the_author_ID number.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Okay, well, you posted code that doesn’t do what you just said you want to do… So.. which is it? Do you want an if-else or not? There’s a lot of ways to do what you’re talking about, but first you have to really define what you want.

    Look at the example code you gave. It used img1 for a value of 1, but img2 for a value of 4. Is that what you want it to do? Or do you want it to use img4 instead?

    If you want author 1 to get img1 and author 42 to get img42, then all you need do is this (for example):

    <img src='img<?php the_author_id(); ?>' />

    If you want to use some other method like the if/else you were describing, then you’d do this:

    <?php
    $atr=get_the_author_ID();
    if ($atr==1)
    echo "img1";
    elseif ($atr==4)
    echo "img2";
    ?>

    Or you could do something like this:

    <?php
    switch (get_the_author_ID()) {
    case 1:
        echo 'img1';
        break;
    case 4:
        echo 'img2';
        break;
    }
    ?>

    But you need to decide what exactly you want to do first. What are the image names? Do you want to name them after the numbers or use some other method of association? Or perhaps you should use Gravatars instead?

    Very helpful post. Thanks for the info. This is what I needed to have author specific content (signature images, etc.) Thanks.

    Example: http://www.churchthatcares.org/blog/

    Very helpful indeed. In the end, I used the Gravatars like this:

    <?php echo get_avatar(get_the_author_email()); ?>

    I’m just telling my authors and editors to use the free Gravatars service and update their photos as they wish.

    By the way, I’m using WordPress MU 2.5.1, check it out here.

    Hi Otto,

    Your code works very well in sidebar and content, but when I tried the same code in header doesn’t works. Could you explain why and how can I resolve? Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘If-Then Statement with the_author_ID()’ is closed to new replies.