get wordpress blog featured image

Following is code for getting the featured image you have defined in the blog index page. Featured image is useful if you want to display it in the site header for the blog or blog posts. Size name can be passed if you have defined the size of images you want to display.

Code to get wordpress blog page featured image

// ndARTS.CA - get_blog_featured_img_url function - START ------------------------------
 
function get_blog_featured_img_url($size=NULL) {
    $page_for_posts = get_option('page_for_posts');
    $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $page_for_posts ) , $size);
    return $imgsrc[0];
}
 
// ndARTS.CA - get_blog_featured_img_url function - END ------------------------------

Two things to keep in mind when using above function is to make sure you have defined the blog page in wordpress settings. Also you need to make sure featured images are supported in the theme.

Define wordpress blog index page

To define the blog page you need to go into Reading under Settings in wordpress admin panel. The field will be called “Posts page”. From here you can define the blog index.
Option to define wordpress blog index page

Turn on featured image setting in theme

Be sure the featured image setting is turned on in your theme otherwise you will not get a featured image box when editing the blog page.
Wordpress featured image editing box
Code which turns on the featured images looks like below…

add_theme_support( 'post-thumbnails' );

Leave a Reply

Your email address will not be published. Required fields are marked *