How to Hide Featured Image in WordPress Post without Plugin

Author:

WordPress is a most popular content management system that provides many user-friendly and SEO-friendly features for users. featured image is one of the features that is displayed next to the post title on the list page and below the title on the post detail page. In this tutorial, we will explore how to hide featured images in WordPress post without any plugins.

How to Hide Featured Image in WordPress Post: without Plugin

Users can create pages, posts and categories using WordPress cms and publish WordPress has an editor to edit the posts or pages so users can insert images, text etc. There is a separate option to upload featured images that display next to the title on the front end.

Most of the time, users insert only featured images with one post but in some cases, they upload other images with the content, and sometimes they upload the same image as featured.

So to remove duplicates and reduce the loading time it required hiding the featured image. Also, there is another chance that the featured image size is larger than the content inner image so the user wants to use the inner image only on the detail page.

How to Change WordPress Featured Image Size

To hide the featured image in WordPress posts there are multiple methods we have already explained a few methods in our post How to Hide featured image on post in WordPress.

In my old post, we have already explained how to hide featured images using Custom CSS and Conditionally Display Featured Images plugin.

Now let’s understand how to hide the featured image in WordPress post without a plugin if the post has an image.

NOTE: In this code featured image will hide only if the post has an image if not have any image then the featured image stays there.

There are the following steps :

Step1: Login to your cpanel or FTP

Step2: Go to your theme folder

Step3: find the content-single.php

In my theme, it was in the template-parts folder. you need to edit the template-parts/content-single.php file

Step4: Search the the_post_thumbnail( ‘full’, [ ‘class’ => ‘class_name ] );

Step5: Replace with the following code

if ( has_post_thumbnail() ) {

$content = get_the_content();

// Improved regular expression for image detection

if ( !preg_match_all(‘/<img[^>]+src=[\'”]([^\'”]+)[\'”]/i’, $content, $matches) ) {

the_post_thumbnail( ‘full’, [ ‘class’ => ‘class_name’ ] );

}

}

In the above code, the class name will be different for your theme and also your content-single.php file location may be different. To find the file and replace the_post_thumbnail() function.

If you encounter any issues, please feel free to leave a comment below. We’re here to help.

Leave a Reply

Exit mobile version