• Resolved bluemoonsound

    (@bluemoonsound)


    Hi everyone !

    I’m trying to obtain two different thumbnails size for my related posts.

    I have two main different categories type for my posts, ”Musical news” and “Podcasts”. The first one categories post thumbnails match with the Jetpack related posts image size (around 16:9). However, I’d like to have a different one size setting for the Podcasts category (a square style).

    Here is the my current code :

    // Make Jetpack related posts thumbnails size conditional
    
    function jetpackchange_image_size ( $thumbnail_size ) {
    	$thumbnail_size['width'] = 350;
    	$thumbnail_size['height'] = 350;
           return $thumbnail_size;
    }
    if ( in_category( array( 363, 365 ) ) ) {
      add_filter( 'jetpack_relatedposts_filter_thumbnail_size', 'jetpackchange_image_size' );
    }

    363 & 365 are my Podcasts categories ID for English and French.
    The conditional tag has no effect on the filter. I tried different ones but without results.

    Anyone have an idea how to figure out that?

    Cheers

    https://www.ads-software.com/plugins/jetpack/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Lisa Schuyler

    (@lschuyler)

    Hi bluemoonsound!

    What if you tried their category slugs instead of their IDs, something like this?

    if ( in_category( 'category-a' ) ) {

    Let me know if that works.

    Thread Starter bluemoonsound

    (@bluemoonsound)

    Hi Lisa!

    Thank you for your reply. Unfortunately that solution doesn’t work.

    Any other ideas? ??

    Plugin Author Brandon Kraft

    (@kraftbj)

    Code Wrangler

    Howdy!

    The issue, I believe, is trying to use a conditional within your functions.php directly. Functions.php always loads, but before “the loop” where the post itself is defined. Since there is no post setup yet, the conditional will never return anything expected.

    What about something like this instead?

    add_filter( 'jetpack_relatedposts_filter_thumbnail_size', 'jetpackchange_image_size' );
    
    function jetpackchange_image_size ( $thumbnail_size ) {
    	global $post;
    	if ( ! $post ){
    		return $thumbnail_size;
    	}
    	else if ( has_category( array(363, 356), $post ) ) {
    		$thumbnail_size['width'] = 350;
    		$thumbnail_size['height'] = 350;
    	}
    	return $thumbnail_size;
    }

    In this example, we’re asking for the global $post variable, which is the post object defined by WordPress. If they’re not one, we’re going to return out early, which shouldn’t happen in my testing, but better safe than sorry. The next conditional uses has_category which is what in_category uses, only in_category assumes the post is setup so it skips the need for you to determine it.

    Long story made short, the a conditional based on the post need to be within the filter’s function itself, not surrounding the add_filter call.

    Cheers!

    Thread Starter bluemoonsound

    (@bluemoonsound)

    Brandon, your code works perfectly !

    Thank you so much ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Conditional thumbnails size for related posts’ is closed to new replies.