• Resolved Doodlebee

    (@doodlebee)


    I’m having a heck of a time in adding a class to the get_the_post_thumbnail() function. As you can see, in the codex page on it, I *should* be able to define an array and add a new class, but not only is it not working, but when I add it, it removes the output completely.

    I have an if/then statement – so “if” something is one way, I need a particular class added, “else” if it’s another way, I need a different class added. I’ve tried several methods to do it, but nothing is working. (I even tried writing my own function to add_filter to it – but no go.)

    My current code is like so:

    // images
         $images = get_post_meta($post->ID, 'images', true);
         $image = $images['imgrep'];
         $title = $images['title'];
         $featured = get_the_post_thumbnail($post->ID, 'medium');
    
         // title representation for top left side of posts
         if(!empty($title)) {
           $title = '<img class="titlerep wp-post-image" src="' . $title . '" alt="" />' . "\n";
           $inuse == 'no';
         } else if(!empty($featured)) {  // if no title rep set, use the featured image
           $title = $featured;
           $inuse == 'yes';
         } else {  // if no title rep set, and no featured image, use the post title text
           $title = '<h2>' . get_the_title() . '</h2>' . "\n";
           $inuse == 'no';
         }
    
         // image representation for right side
         if(!empty($image)) {
           $image = '<img class="imgrep wp-post-image" src="' . $image . '" alt="" />' . "\n";
         } else if(!empty($featured) && $inuse == 'no') {  // if no title rep set, use the featured image
           $image = $featured;
         }

    So I’ve been trying to simply change the $featured variable at the top, and instead of leaving it there, pulling it into the conditionals and adding the class. ($title should have ‘titlerep’ added, and $image should have ‘imagerep’ added). I’ve tried just adding “'class=titlerep'” to the end, writing an array of values where “'class' => 'titleprep'” is in there and added to the end,even tried popping in a small function for the apply_filter thing – and like I said, nothing is working. Anyone have any ideas on how to do this?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • get_the_post_thumbnail uses wp_get_attachment_image internally to generate the html. Looks like wp_get_attachment_image doesn’t process merging the attr with the default attributes (specifically, it doesn’t ADD any class you specify to the default class, ‘attachment-$size’, it just replaces it). Naughty!

    Suggest you open a ticket in trac and try to persuade the core team to fix it.

    $featured = str_replace( 'class="', 'class="titlerep ', $featured );

    🙂

    Thread Starter Doodlebee

    (@doodlebee)

    aha! Awesome! Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add additional class to get_the_post_thumbnail()?’ is closed to new replies.