Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Thread Starter DDT

    (@ddt)

    @keesiemeijer, tx

    As i read it every image is being resized, right?

    In my case i want the resizing to be related to the custom post type something like

    function alter_image(){
    	global $post;
    	if($post->post_type == 'reviews') {
    	add_image_size( 'review-thumbnail', 150, 200, true );
    	}
    }
    add_action('edit','alter_image');

    But that didn’t work. Just adding
    add_image_size( 'review-thumbnail', 150, 200, true );
    works but now every uploaded image has a ‘review-thumbnail’

    Any suggestions?

    Moderator keesiemeijer

    (@keesiemeijer)

    if you have multiple post types, maybe this will help
    (not tested)(in the loop)

    <?php
    $post_type = get_post_type(); // get's the post type of a post in the loop
    $args=array(
      'public'   => true,
      '_builtin' => false
    );
    $output = 'names'; // names or objects
    $operator = 'and'; // 'and' or 'or'
    $post_types = get_post_types($args,$output,$operator); // gets all post types you registered
    $post_types = array_values($post_types); // makes it a numerically indexed array
    
    if(in_array( $post_type, $post_types)){ // post is a custom post type -> show post type size thumbnail
      $custom_post_thumbnail = get_the_post_thumbnail($post->ID, $post_type.'-thumbnail');
      if($custom_post_thumbnail != ''){ // there is a custom post thumbnail size
        echo $custom_post_thumbnail;
      } else { // no custom post thumbnail size -> show normal thumbnail
        echo get_the_post_thumbnail($post->ID);
      }
    } else { // show normal thumbnail for posts and pages
      echo get_the_post_thumbnail($post->ID);
    }
    ?>

    You still have to add the image sizes manually in your theme’s functions.php
    If you have custom post types review and movies:

    add_image_size( 'review-thumbnail', 150, 200, true );
    add_image_size( 'movies-thumbnail', 400, 9999 );

    Moderator keesiemeijer

    (@keesiemeijer)

    And for the default size for normal post thumbnails use: https://codex.www.ads-software.com/Function_Reference/set_post_thumbnail_size

    if ( function_exists( 'add_theme_support' ) ) {
    	add_theme_support( 'post-thumbnails' );
            set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions
    }
    
    if ( function_exists( 'add_image_size' ) ) {
    	add_image_size( 'review-thumbnail', 150, 200, true );
            add_image_size( 'movies-thumbnail', 400, 9999 );
    }

    Hello keesiemeijeer!

    I guess that your snippet does not prevent to be generated images in this custom sizes for each image uploaded.

    There is the point.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to set different thumbnail sizes per custom post type?’ is closed to new replies.