Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Display featured image before post title

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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?

>Solution :

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 );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading