Disable showing the featured image on pages in GeneratePress. Primarily for use when the featured image breaks the page layout.
A featured image is useful for SERP (Search Engine Results Page) but may break your site’s layout. In this case simply don’t display the featured image!
I developed a site with the initial design from GeneratePress’ Site Library. My colleague was doing SEO work on the content and suggested adding featured images to each page as they would appear in search results. As I added featured images I saw that they broke the page layout as they were being displayed between the header and the page’s Page Hero element (a GeneratePress Premium feature).
I therefore had to find a way to set the featured image (so that Yoast would pick it up and make it available to search engines) but not display it.
GP Premium is an option
The site had the GP Premium plugin installed and active for the Elements/Page Hero feature and I could have activated the Disable Elements feature. I chose not do this as it would have required disabling the featured image on each and every page. Doing it in one go in code would be much easier.
Which hook
When faced with a large codebase I generally start at the frontend and work back towards the source code. I look for class names in the markup and search the codebase for them, and then work through the function call stack to find which function controls the feature I am interested in.
The featured image <img> tag was wrapped in:
<div class="featured-image page-header-image grid-container grid-parent">
....
</div>
Searching for featured-image I found the generate_featured_page_header_area() function in generatepress/inc/structure/featured-images.php. This function is called by two others and one of those passes the page-header-image class as a parameter.
That function is called via:
add_action( 'generate_after_header', 'generate_featured_page_header', 10 );
so I can use remove_action() to disable it. I added my own add_action() to run earlier on the same hook. The generate_featured_page_header() function only runs on pages so I don’t need to check for pages in my code but, of course, it would be easy to add checks if you only want to remove the featured image from some pages.
Leave a Reply