Add New Image Size and Use it On Theme

Hi,

I want add a new image size and I want use it as homepage posts featured image. My image size will be like below. Please help me I tried everything what I know. My website: https://www.soruncozumu.com

add_image_size( 'home-featured', '311', '9999', false );

Hey @SeLoRe,

Please add this code to your functions.php and replace the image names entry-cropped-fullwidth, entry-cropped, entry-fullwidth and entry with your image name home-featured

function x_featured_image( $cropped = '' ) {

  $stack     = x_get_stack();
  $fullwidth = ( in_array( 'x-full-width-active', get_body_class() ) ) ? true : false;

  if ( has_post_thumbnail() ) {

    if ( $cropped == 'cropped' ) {
      if ( $fullwidth ) {
        $thumb = get_the_post_thumbnail( NULL, 'entry-cropped-fullwidth', NULL );
      } else {
        $thumb = get_the_post_thumbnail( NULL, 'entry-cropped', NULL );
      }
    } else {
      if ( $fullwidth ) {
        $thumb = get_the_post_thumbnail( NULL, 'entry-fullwidth', NULL );
      } else {
        $thumb = get_the_post_thumbnail( NULL, 'entry', NULL );
      }
    }

    switch ( is_singular() ) {
      case true:
        printf( '<div class="entry-thumb">%s</div>', $thumb );
        break;
      case false:
        printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
          esc_url( get_permalink() ),
          esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
          $thumb
        );
        break;
    }

  }

}

If you want to make the change to your home page only, insert a condition above the switch line like this

    if ( is_home() ) {
        $thumb = get_the_post_thumbnail( NULL, 'home-featured', NULL );
    }

    switch ( is_singular() ) {

Make sure you’ve regenerated your thumbnails before you do this.

Hope that helps.

1 Like

Thank you so much for your interest. Yes I want change only home page thumbnail image. My last code below and it provided my purpose. But I want ask one more thing, you told me replace the image names with my image name home-featured is this necessary ?

function x_featured_image( $cropped = '' ) {

  $stack     = x_get_stack();
  $fullwidth = ( in_array( 'x-full-width-active', get_body_class() ) ) ? true : false;

  if ( has_post_thumbnail() ) {

    if ( $cropped == 'cropped' ) {
      if ( $fullwidth ) {
        $thumb = get_the_post_thumbnail( NULL, 'entry-cropped-fullwidth', NULL );
      } else {
        $thumb = get_the_post_thumbnail( NULL, 'entry-cropped', NULL );
      }
    } else {
      if ( $fullwidth ) {
        $thumb = get_the_post_thumbnail( NULL, 'entry-fullwidth', NULL );
      } else {
        $thumb = get_the_post_thumbnail( NULL, 'entry', NULL );
      }
    }

    if ( is_home() ) {
        $thumb = get_the_post_thumbnail( NULL, 'home-featured', NULL );
    }

    switch ( is_singular() ) {
      case true:
        printf( '<div class="entry-thumb">%s</div>', $thumb );
        break;
      case false:
        printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
          esc_url( get_permalink() ),
          esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
          $thumb
        );
        break;
    }

  }

}

Hi,

No need to replace, just add this line above switch


    if ( is_home() ) {
        $thumb = get_the_post_thumbnail( NULL, 'home-featured', NULL );
    }

Don’t forget to regenerate your thumbnails.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.