how to use first post image as thumbnail on wordpress

You can try this function, in your functions.php file. // automatically retrieve the first image from posts function get_first_image() { global $post, $posts; $first_img = ”; ob_start(); ob_end_clean(); $output = preg_match_all( ‘/<img .+src=[\'”]([^\'”]+)[\'”].*>/i’, $post->post_content, $matches ); $first_img = $matches[1][0]; if ( empty( $first_img ) ) { // defines a fallback imaage $first_img = get_template_directory_uri() . … Read more

Print specific image size in galley loop

You can define your own post thumbnail sizes add_theme_support( ‘post-thumbnails’ ); set_post_thumbnail_size( 150, 150 ); This means that you change the thumbnail size overall. Or you can define custom image sizes add_action( ‘after_setup_theme’, ‘wpdocs_theme_setup’ ); function wpdocs_theme_setup() { add_image_size( ‘category-thumb’, 300 ); // 300 pixels wide (and unlimited height) add_image_size( ‘homepage-thumb’, 220, 180, true ); … Read more

Set Post Image Using Shortcode

The Shortcode API is more geared for altering post content rather than performing actions in response to content, and as such tend to require more creativity to alter other post data – though it can still be done. Ultimately, using a shortcode to set a featured image is kind of bizarre considering that you can … Read more

What is the standard featured image size?

WordPress has a few default sizes: Thumbnail ( Defaults to 150×150 ) Medium ( Defaults to 300×300 ) Large ( Defaults to 1024×1024 ) Full ( Defaults to Original Image Size ) Of course, these can all be changed by visiting the Settings -> Media page in the Admin Panel of your installation. That being … Read more

Display recent posts with thumbnail

the_post_thumbnail() can only be used in “the Loop”. What you need is get_the_post_thumbnail(). (See details and code examples on the code reference page). So something like: foreach( $recent_posts as $recent ){ echo ‘<li>’; echo get_the_post_thumbnail( $recent[‘ID’], ‘size’, array( ‘class’ => ‘alignleft’ ) ); echo ‘<a href=”‘ . get_permalink($recent[“ID”]) . ‘”>’ . ( __($recent[“post_title”])).'</a></li> ‘; } … Read more

Recent posts with featured image or fallback image with permalink

Here’s the relevant part of your code that should work: // This will make a URL like http://yoursite.com/path/to/fallback.png $fallback_image = site_url( ‘/path/to/fallback.png’ ); $fallback_image = “<img src=”https://wordpress.stackexchange.com/questions/224740/{$fallback_image}” />”; foreach( $recent_posts as $recent ){ echo ‘<div class=”sidebar-entries”>’; $featured_image = get_the_post_thumbnail( $recent[‘ID’], ‘sidebar-thumb’, array( ‘class’ => ‘sidebar-image’ ) ); if ( ! strlen( $featured_image ) ) { … Read more