array set title and alt in the_post_thumbnail

Try using get_the_title instead of the_title – from the Function Reference: the_title – Display or retrieve the current post title with optional markup. get_the_title – Retrieve post title. You might notice that the_title says “Display or retrieve” – and it’s true, you can pass false to the third parameter of the_title to get it’s output … Read more

Check if the image size is available and if not use ‘full’ image size

I don’t see why something like this wouldnt work! $med_post_thumb = get_the_post_thumbnail($post->ID, ‘medium_post_thumbnails’, array(‘class’ => ‘img-fluid’)). ‘</a></div>’; $full_post_thumb = get_the_post_thumbnail($post->ID, ‘FULL SIZE GOES HERE’, array(‘class’ => ‘img-fluid’)). ‘</a></div>’; if ($med_post_thumb == NULL) { echo $full_post_thumb; } else { echo $med_post_thumb; }

How to retrieve the current post’s generated featured image size?

Have a look to wp_get_attachment_image_src(), which gives precisely data you need (URL and dimensions). Which help to figure out your problem. sample code $image_attributes = wp_get_attachment_image_src( $attachment_id = 10 ); if ( $image_attributes ) : ?> <img src=”https://wordpress.stackexchange.com/questions/302831/<?php echo $image_attributes[0]; ?>” width=”<?php echo $image_attributes[1]; ?>” height=”<?php echo $image_attributes[2]; ?>” /> <?php endif; ?> The return … Read more

Get Post’s first image using Short-code

I would rewrite a simple shortcode to retrieve only post images: add_shortcode( “postImages”, “stack_309224_attachhment” ); function stack_309224_attachhment($atts){ extract(shortcode_atts( array( ‘id’ => 1, // default id of the post “max” =>4 ,// default max number of images to display “featured”=>”false” // if to retrieve the post featured image or not, default false ), $atts )); ob_start(); … Read more

Remove Featured Image & All Media Uploaded to the Post

You can try using the function get_attached_media() like this: add_action( ‘before_delete_post’, ‘wps_remove_attachment_with_post’, 10 ); function wps_remove_attachment_with_post( $post_id ) { /** @var WP_Post[] $images */ $images = get_attached_media( ‘image’, $post_id ); foreach ( $images as $image ) { wp_delete_attachment( $image->ID, true ); } } Note that this will permanently delete all the image files related to … Read more

Should I not compress my jpeg files before uploading to avoid double compression?

If you want to showcase high-quality images on your website, then you can turn off image compression in WordPress. You can do that by adding this part of code in your functions.php : add_filter(‘jpeg_quality’, function($arg){return 100;}); Or you can create your plugin : <?php /** * Plugin Name: Remove WordPress Image Compression * Plugin URI: … Read more

Display related posts without a plugin

I slightly modified your code and tested on my WordPress installation. If you using a filter, like ‘the_content’, which recieves a content as an argument, you must also return this content back. Here is working version of your function with comments. //this filter recieves $content, which you should always return, //otherwise you will not see … Read more

Add class to custom thumbnail

The third parameter you can pass to get_the_post_thumbnail is $attributes, which you can use to pass custom HTML attributes including classes. $thumbnail = get_the_post_thumbnail( get_the_ID(), // Post ID. ‘thumbnail’, // Image size. array( // Attributes ‘class’ => ‘my-custom class-names’, // Classes go here. ) ); Note that this will override the default image classes that … Read more