Site logo

Archived topic

Thumbnail with category name overlay

7 replies · Started by sheik on March 15, 2017

Viewing posts 1–8 of 8

Hi,

I'm looking for some pointers to add the category name of a post onto the featured image - sort of like an overlay.
Much appreciate any input.

Can this be done with GP Hooks?

Hi there,

You could use a function like this:

add_filter( 'post_thumbnail_html','tu_add_category_featured_image' );
function tu_add_category_featured_image( $output ) {
	$return = $output;
	$cat = get_the_category( get_the_ID() );
	if ( isset( $cat ) && ! is_singular() ) {
		foreach( $cat as $cat_name ) {
			$return .= '<div class="featured-image-category">' . $cat_name->name . '</div>';
		}
	}
	
	return $return;
}

Then style it with some CSS:

.post-image {
    position: relative;
}

.featured-image-category {
    position: absolute;
    top: 10px;
    left: 10px;
}

Thanks for the (usual) quick reply. I'm a little green here - do I add that code to functions.php in my child theme? or to a GP Hook?
I (at least) know where the CSS goes :-)

Thanks!

Thanks fellas. I've decided to go another route as other plugins seem to interfere.

No problem :)

This code is great, except I don't think it works on mobile. Any advice Tom?

Hi @Marcin,

This code is great, except I don’t think it works on mobile. Any advice Tom?

It should work for mobile as well. Perhaps with a bit of CSS changes to your preference on mobile. (@media rule for mobile).

Can you open up a new topic for this? So you could use the private information text field to provide the site details for us to check. Let us know.

This archived topic is closed to new replies.