WordPress.org

Support

Support » How-To and Troubleshooting » How to change user image depending on category?

How to change user image depending on category?

  • On my site: http://www.taibros.net I currently have it set up so that I have a line in each author (2 authors for my blog) profile that calls an image for the author, and then I use a tag to show the author profile which results in the image for the author of the post.

    I was hoping to have this image change depending on what category the post was made in. So a “thinking” picture would show up for my “musings” category and so on and so forth. What would be the best way to do something like this?

    Where I wouldn’t need to do anything other than the initial setup of code and it would just take care of itself afterwards?

Viewing 8 replies - 1 through 8 (of 8 total)
  • How would you like something really indepth for this?

    <?php //Place this in The Loop.
    $img_path = 'images/'; //Change to image dir.
    $imgext = 'jpg'; //Change to image extension.

    $author = strtolower(sanitize_title(get_the_author_nickname()));
    $author_img = get_settings('siteurl') . '/' . $img_path . $author . '_' . $cat . '.' . $imgext;
    $imgsize = getimagesize($author_img);
    ?>

    Now, for your <img> tag:

    <img src ="<?php echo $author_img; ?>" alt="<?php the_author(); ?>" title="<?php the_author(); ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[0]; ?>" />

    Create your images using the following naming convention:

    {author-nickname}_{catID}.jpg

    ‘author-nickname’ should be obvious; uses dashes for spaces if a nickname has any, and keep the name in lowercase. ‘catID’ is the numeric category ID. So with my nickname and the #3 category, I’d have:

    kaf_3.jpg

    This lacks a few niceties, for example a default author image when a category lacks one, but it should get you going.

    So, anyone think this is plugin material? :)

    By the way, if you’re not using this only in a category.php template, I’d suggest this as your img tag:

    <?php if(is_category()) : ?>
    <img src ="<?php echo $author_img; ?>" alt="<?php the_author(); ?>" title="<?php the_author(); ?>" width="<?php echo $imgsize[0]; ?>" height="<?php echo $imgsize[0]; ?>" />
    <?php endif; ?>

    haha thanks Kafkaesqui! So would I put the image tag in my author profile? Or would I want to put that into my code where I want my image to go?

    This would definitely be pretty sweet as a plugin to make it easier to use in the index. One way for it to check for a default would be to define an image as the default, and then have it check to see if the file exists when searching for category by incrementing the number. If there’s no pic for category 3, then it’ll use the image that you selected. Heh, if only my PHP coding were a little better.

    Thanks again!

    Check the first line:

    <?php //Place this in The Loop.

    The plugin (which I’ve apparently volunteered to write! 🙂 would need two levels of default images: 1 for when no image for an author is available, and one for when no author-category image exists. The degrade would be: if no author-cat image => author image, if no author image => generic image (if no image at all, don’t do anything).

    I’m thinking of these as user parameters in the plugin’s template function:

    * image path
    * generic image filename
    * author-format (login, firstname, nickname, etc.)
    * cat type (ID or nicename)
    * file extension
    * image width (to override plugin)
    * image height (ditto)
    * css class
    * exclude categories

    That sounds quite a snazzy little plugin – Kaf is the implementation of a gravatar, favatar simpler and could this plugin work with it?

    so: if no gravatar>author image from specified url, if no author-image>generic image from specified url

    hehe thanks for offering to write a plugin! 🙂

    Jinsan, Simpler? Not really. As for working along side (gr|f)avatars, I’d have to look at those resources, but as with anything along these lines, it’s only a matter of programming. Which means it should be possible, barring the programmer’s limitations…

    lawtai, Go ahead and chuckle!

    I need some help. I tried to use this for my own blog and keep getting an error 500.

    Here is my (edited) code:

    <?php //Place this in The Loop.
    $img_path = ‘http://neo-ilove.com/images/’; //Change to image dir.
    $imgext = ‘gif’; //Change to image extension.
    $author = strtolower(sanitize_title(get_the_author_nickname()));
    $author_img = get_settings(‘http://neo-ilove.com/neopets’) . ‘/’ . $img_path . $author . ‘.’ . $imgext;
    $imgsize = getimagesize($author_img);
    ?>

    ^^^Added after header include tag

    <?php if(is_category()) : ?>
    <img src =”<?php echo $author_img; ?>” alt=”<?php the_author(); ?>” title=”<?php the_author(); ?>” width=”<?php echo $imgsize[0]; ?>” height=”<?php echo $imgsize[0]; ?>” />
    <?php endif; ?>

    I just want an image displayed per author for now.

    Many thanks 😀

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to change user image depending on category?’ is closed to new replies.