SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Automatically add a featured image

    Is there a way to have a thumbnail image automatically added to a post when it is given a particular category?

    For example, if I was to give a post a category 'Music', when it's published it would automatically have a little thumbnail image of a guitar in the post without me having to do anything else.

    How would I achieve this?

  2. #2
    Mazel tov! bronze trophy kohoutek's Avatar
    Join Date
    Aug 2004
    Location
    Hamburg, Germany
    Posts
    4,248
    Mentioned
    30 Post(s)
    Tagged
    0 Thread(s)
    You can do that by following the instructions shown here.
    Maleika E. A. | Rockatee | Twitter | Dribbble



  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds like it'll do the trick, thanks.

    One more stupid question, is it just a case of wrapping that 'foreach' chunk of code in a function, dropping it into functions.php then calling it from within the relevant templates to get the image to display?

    Sorry, i'm new to all this

  4. #4
    Quality not quantity. bo5ton's Avatar
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    654
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php 
    foreach((get_the_category()) as $category) { 
        echo 
    '<img src="http://example.com/images/' $category->cat_ID '.jpg" alt="' $category->cat_name '" />'

    ?>
    This code you can put anywhere in any of your templates, wherever you want your image to appear. You don't need to have it as a function in functions.php.

    Pretend you're putting your <img> element in your template like normal, but put that entire php code instead.

    This...
    PHP Code:
    $category->cat_ID '.jpg" 
    ...represents the image file name. So depending on the category id it will output something like 1234.jpg - so you would have your category image inside like:

    Code:
    http://examples.com/images/1234.jpg
    PHP Code:
    alt="' . $category->cat_name . '" 
    This represents your alt tag and will use the category name in here.

    You will want to be careful that you only have 1 category per post or you could have a number of images being displayed.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, thank you very much for the help.

  6. #6
    SitePoint Member mapetshi's Avatar
    Join Date
    Jan 2012
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kohoutek View Post
    You can do that by following the instructions shown here.
    Thanks for the link


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •