Forums

If-Then Statement with the_author_ID() (6 posts)

  1. sirenique
    Member
    Posted 1 month ago #

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

  2. Otto42
    Moderator
    Posted 1 month ago #

    Use the first example with get_the_author_ID instead of the_author_ID.

  3. sirenique
    Member
    Posted 1 month ago #

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

  4. Otto42
    Moderator
    Posted 1 month ago #

    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.

  5. sirenique
    Member
    Posted 1 month ago #

    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.

  6. Otto42
    Moderator
    Posted 1 month ago #

    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?

Reply

You must log in to post.

About this Topic