Home > Blockchain >  Display featured image before post title
Display featured image before post title

Time:11-24

I've been trying to get the featured image to display before post content. I'm new to php and this is my code:

function featured_image_before_title($title, $id) { 
 if (get_post_type($id) === 'post') {
        $featuredimage = get_the_post_thumbnail();
        $title = $featuredimage . $title;
        }
    return $title;
}

add_filter( 'the_title', 'featured_image_before_title', 10,2 );

The above works on front end but when I go to Admin > Posts > All posts the Title column displays a bunch of html markup before the actual post title.

The html markup is the thumbnails sizes like below:

<img width="1280" height="150" src="http://example.com/wp-content/uploads/image.jpg" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="https://example.com/wp-content/uploads/image.jpg 1280w, https://example.com/wp-content/uploads/image-300x83.jpg 300w, https://example.com/wp-content/uploads/image-1024x284.jpg 1024w, https://example.com/wp-content/uploads/image-768x213.jpg 768w, https://example.com/wp-content/uploads/image-1080x300.jpg 1080w, https://example.com/wp-content/uploads/image-1280x356.jpg 1280w, https://example.com/wp-content/uploads/image-980x272.jpg 980w, https://example.com/wp-content/uploads/image-480x133.jpg 480w, https://example.com/wp-content/uploads/image-600x167.jpg 600w" sizes="(max-width: 1280px) 100vw, 1280px" /> POST TITLE 

How can I insert the featured image before the post title?

CodePudding user response:

You have to use is_admin(). try the below code.

function featured_image_before_title($title, $id) { 

    if( is_admin() )
        return $title;

    if ( get_post_type( $id ) === 'post') {
        $featuredimage = get_the_post_thumbnail();
        $title = $featuredimage . $title;
    }
    return $title;
}
add_filter( 'the_title', 'featured_image_before_title', 10, 2 );
  • Related