Delete post media except featured image

If someone has clashed with such problem, I solved it in this way: $attachments = get_posts( array( ‘post_type’ => ‘attachment’, ‘posts_per_page’ => -1, ‘post_status’ => ‘any’, ‘post_parent’ => $post_id ) ); $thumbId = get_post_thumbnail_id($post_id); foreach ( $attachments as $attachment ) { if($thumbId != $attachment->ID) wp_delete_attachment( $attachment->ID, true ); }

How to get the real thumbnail size?

wp_get_attachment_metadata – Retrieve attachment meta field for attachment ID. $image_meta = wp_get_attachment_metadata( $attachment_id, $unfiltered ); As you can see, the width x height is nested in sizes for each size. Array ( [width] => 2400 [height] => 1559 [file] => 2011/12/press_image.jpg [sizes] => Array ( [thumbnail] => Array ( [file] => press_image-150×150.jpg [width] => 150 … Read more

Display Random Posts with thumbnail instead of just a title of the post

You can get the post thumbnail HTML using the function get_the_post_thumbnail(), so you just need to replace get_the_title() with that function: $string .= ‘<li><a href=”‘. get_permalink() .'”>’. get_the_post_thumbnail() .’ </a></li>’; By default the image will be the size defined by your theme, but you use any of the other available sizes by passing the size … Read more