1

I have set a new size (width/height) for Featured Image and have also set the option to use a Default Image(Fallback image).

The size used for the Featured Image: 1170/550 , the size for the Default Image is not set atm.

My two questions are:

  1. How can I set the (same) size as used for the Featured Image to my Default Image.
  2. Is it possible to make the size for the Default Image dynamically, meaning by that:
    When changing the size for Featured Image the Default Image(my Fallback img) will then automatically use the size as set for the Featured Image.

The code I have/use right now:

<div class="post-thumbnail">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php 
    if ( has_post_thumbnail() ) {
        the_post_thumbnail( 'featured-image' );
    } else {
        ?>
        <img src="<?php bloginfo('template_directory'); ?>/images/thumb_featured_img.jpg" alt="<?php the_title(); ?>" />
        <?php
    } ?>
</a>
</div>

The code used for the Featured Image:

add_image_size( 'featured-image', 1170, 550, array( 'center', 'center' ) );

Is there a way to achieve this, and if so, how?

  • you mean to say that your default image i.e thumb_featured_img.jpg should be of 1170*550??? – terminator Feb 9 '16 at 12:17
  • yes exactly but be dynamic (same as featured-image size ) not hard coded – user2610000 Feb 9 '16 at 12:25
  • rephrase question? is really confusing what are you trying to achieve. – majick Feb 9 '16 at 12:25
  • I have set featured image size to 1170*550 and have fallback image without any image size defined now I want the fallback image should have the same size as the featured image i.e. 1170*550, but it should be dynamic if featured image is reset then the fallback image should adjust accordingly – user2610000 Feb 9 '16 at 12:30
  • you are using the images directory inside your themes folder to pull the featured image in case featured image doesn't exit. Won't it be better to put the image of size 1170*550 in the images folder i.e to be used for the featured image. – terminator Feb 9 '16 at 12:36
0

You can get the width and height of the image size set back from the global $_wp_additional_image_sizes array via the image size slug, so that:

global $_wp_additional_image_sizes;
$size = 'featured-image';
$width  = $_wp_additional_image_sizes[$size]['width'];
$height = $_wp_additional_image_sizes[$size]['height'];

Then in your template just change the image attributes:

<img src="<?php bloginfo('template_directory'); >/images/thumb_featured_img.jpg" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="<?php the_title(); ?>" />
| improve this answer | |
  • more info with examples: codex.wordpress.org/Function_Reference/… – majick Feb 9 '16 at 12:34
  • Thanks for the comment but its not working actually my question may not be very clear add_image_size() actually crops the image I want some type of coping not re-sizing – user2610000 Feb 9 '16 at 12:46
  • @majick, and how should this help/answer the OP question? – Charles Feb 9 '16 at 13:14
  • he says he wants the fallback image to adjust according to the size of the featured image which may change, and this does exactly that. the display output of a single static fallback image is adjusted to match the featured image size if no featured image exists... if something else is being asked for I really can't see what. – majick Feb 10 '16 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.