0

So as the title says im trying to add a div class to the thumbnails from a single category. Im trying to use this code:

 if (in_category( 'Expired' )) : ?><div class="expire">

This is the single post code im trying to add it to:

 <?php
        if(has_post_thumbnail())  {
            the_post_thumbnail(
                array($theme->get_option('featured_image_width_single'), $theme->get_option('featured_image_height_single')),
                array("class" => $theme->get_option('featured_image_position_single') . " featured_image")
            );
        }
    ?>

And this is the div class ive created in the stylesheet:

.expire {
position: absolute;
bottom: 10px;
background:url(images/sample-expired.png) no-repeat ;
width: 100px;
height:60px;
text-align: center;
padding: 4px;
border-radius: 10px;
left: 2px;
}

Any ideas where in the code I should paste this? or any help as to getting this implemented? Thanks heaps

3
  • What is the final HTML supposed to look like? Do you want to add the class to the image tag?
    – Sven
    Commented Dec 13, 2017 at 3:22
  • Im just wanting a banner image overlayed on the thumbnail for the category "expired", ive been trying for weeks to acheive it, usually with trial and error i can move around the code and get things implemented, but this has me stumped. Commented Dec 13, 2017 at 3:46
  • This is the page if it helps anyone ez007.net/FreeSamplesAustraliaByMailOnly/expired/… Commented Dec 13, 2017 at 3:56

1 Answer 1

0

To wrap the thumbnail wit I would do something like this:

First. Create the expire class string

$expire_class = in_category('expired') ? 'expire' : '';

Then, wrap your post thumbnail code with this:

<div class='<?php echo $expire_class; ?>' > 
[put your thumbnail code here]
</div>

So, it looks like this:

<?php
$expire_class = in_category('expired') ? 'expire' : '';
if(has_post_thumbnail())  {
        ?><a href="<?php the_permalink(); ?>"><div class='<?php echo $expire_class; ?>' > <?php the_post_thumbnail(
            array($theme->get_option('featured_image_width'), $theme->get_option('featured_image_height')),
            array("class" => $theme->get_option('featured_image_position') . " featured_image")
        ); ?></div></a><?php  
    }
?>
7
  • Thanks mate, where do i put the expire class string exactly? Commented Dec 13, 2017 at 3:25
  • Just above if(has_post_thumbnail())
    – hamdirizal
    Commented Dec 13, 2017 at 3:27
  • how does this look? it seems to have made no change? Commented Dec 13, 2017 at 3:31
  • ugh i cant work out how to format the comment as code, usually pressing control K works but not in comments for some reason... sorry im being a pain in the ass lol Commented Dec 13, 2017 at 3:35
  • It seems that you've mixed PHP and HTML code. Please check my edited answer.
    – hamdirizal
    Commented Dec 13, 2017 at 3:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.