- This topic has 13 replies, 3 voices, and was last updated 1 year, 8 months ago by
Leo.
-
AuthorPosts
-
October 26, 2020 at 11:26 am #1505174
George
I have set up a header element that is displayed in all posts and pages. The element shows the featured image and the title when a page is loaded and the post title with the post meta and category when a post is loaded. All post meta is being displayed through header element template tags.
I want to be able to programmatically disable the header element whenever a featured image is not present in a post or page. What I mean but that is that I want it to be completely deactivated (not hidden with CSS) so that normal single post meta tags are displayed instead as if the header element was not added.
I tried disabling the
Featured Image / Page Header
option on the page but that doesn’t seem to deactivate the header element.October 26, 2020 at 11:30 am #1505182Leo
StaffCustomer SupportHi George,
Is this what you are looking for?
https://generatepress.com/forums/topic/hide-hero-header-on-specific-pages/#post-1032869Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 26, 2020 at 11:36 am #1505189George
Yes, Leo, that’s EXACTLY what I want! I only had to add
!
to the first thumbnail conditional.Thanks!
October 26, 2020 at 11:43 am #1505196Leo
StaffCustomer SupportNo problem ๐
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 26, 2020 at 11:49 am #1505202George
Hi Leo, sorry for opening this again. This only works for a post, how should I modify to work for a page as well?
October 26, 2020 at 12:01 pm #1505211Leo
StaffCustomer SupportI wonder if this would work:
has_post_thumbnail( $post->ID )
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 26, 2020 at 12:05 pm #1505213George
Nope…
October 26, 2020 at 12:10 pm #1505221George
What about this
_generate-disable-post-image
, should it be something else for pages?October 26, 2020 at 12:26 pm #1505241Elvin
StaffCustomer SupportHi,
Try this snippet.
add_filter( 'generate_header_element_display',function($display){ if( !has_post_thumbnail() || get_post_meta( get_the_ID(), '_generate-disable-post-image', true) === 'true' ){ $display = false; } return $display; },10,1);
A wise man once said:
"Have you cleared your cache?"October 26, 2020 at 12:35 pm #1505244George
Hi Elvin, the last code only works for pages now! This is what I have so far:
add_filter( 'generate_header_element_display', function( $display, $element_id ) { if ( 80 === $element_id ) { // Only target specific Element if ( !has_post_thumbnail(get_the_ID()) || get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' ) { $display = false; } } return $display; }, 10, 2 );
I have also disabled featured images for single posts and pages from the Customizer.
October 26, 2020 at 12:40 pm #1505251George
Only using
has_post_thumbnail()
in the conditions works, I think the rest of the condition has an issue?October 26, 2020 at 12:42 pm #1505259Elvin
StaffCustomer SupportHi,
I’ve updated the code from the previous reply.
Can you try and see if it works for you?
Here’s a temporary demo of it working. demo site
A wise man once said:
"Have you cleared your cache?"October 26, 2020 at 12:49 pm #1505271George
Hi, apologies, had a setting that was messing it up. This works for me:
add_filter( 'generate_header_element_display', function( $display, $element_id ) { if ( 80 === $element_id ) { // Only target specific Element if ( !has_post_thumbnail($post->ID) || get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' ) { $display = false; } } return $display; }, 10, 2 );
Basically what Leo said here:
https://generatepress.com/forums/topic/disable-header-element-when-no-featured-image-is-uploaded/#post-1505211Apologies again, it seems to be working now, thank you both, amazing support as always!
October 26, 2020 at 12:52 pm #1505272Leo
StaffCustomer SupportGlad to hear!
I’ll get this added to docs ๐
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.