- This topic has 7 replies, 3 voices, and was last updated 3 years, 2 months ago by Tom.
-
AuthorPosts
-
January 26, 2021 at 2:59 am #1633892Natalie
I have code in my functions.php based on the information here:
https://docs.generatepress.com/article/generate_header_element_display/
my code includes these lines (and lots of similar lines)
global $post; $legal_pp = array(884,2821)); if ( 1945 === $element_id && ((in_array($post->post_parent, $legal_pp) ) || (in_array($post->ID , $legal_pp)))) { return true; }
This works fine.
I have debug turned on in my local install.
I have noticed that if I’m in the admin and don’t have the website open at all, I am getting error messages :
“Trying to get property ‘post_parent’ of non-object” for the lines in functions.php with the code above ( and similar lines)
What I don’t understand is why that filter (generate_header_element_display) is being called when I am in admin as shouldn’t it only apply to the frontend?
I can resolve the whole problem by putting :
if(is_admin()) return true;
as the first line in my function. I’m happy to do that, but would it be a good idea to document that on this page?
https://docs.generatepress.com/article/generate_header_element_display/January 26, 2021 at 6:39 am #1634155DavidStaffCustomer SupportHi there,
can you share the full code including the add_filter – so we can take a look
January 26, 2021 at 6:45 am #1634164NatalieThanks – here’s my full code
function sav_add_headers($display, $element_id) { // if a page header is already defined then return if ( $display ) return true; global $post; if ( 1993 === $element_id && is_page_template( 'saville-aptitude-page.php' ) ) { return true; } $legal_pp = sav_build_id_array(array(884)); if ( 1945 === $element_id && ((in_array($post->post_parent, $legal_pp) ) || (in_array($post->ID , $legal_pp)))) { return true; } $strength_pp = sav_build_id_array(array(374)); if ( 1947 === $element_id && ((in_array($post->post_parent, $strength_pp) ) || (in_array($post->ID , $strength_pp)))) { return true; } $wave_pp = sav_build_id_array(array(376, 1267)); if ( 1953 === $element_id && ((in_array($post->post_parent, $wave_pp) ) || (in_array($post->ID , $wave_pp)))) { return true; } $consult_pp = sav_build_id_array(array(2602)); if ( 1984 === $element_id && ((in_array($post->post_parent, $consult_pp) ) || (in_array($post->ID , $consult_pp)))) { return true; } return $display; } add_filter( 'generate_header_element_display', 'sav_add_headers', 10, 2 );
and the sav_build_id_array function:
function sav_build_id_array($page_ids) { $new_page_ids = $page_ids; foreach ($page_ids as $key => $value) { $da_id = apply_filters( 'wpml_object_id', $value, 'page', FALSE, 'da' ); if($da_id > 0) $new_page_ids[] = $da_id; } return $new_page_ids; }
January 26, 2021 at 10:03 am #1634584TomLead DeveloperLead DeveloperHi there,
We run Elements in the Dashboard so we can apply some of their settings while editing pages. For example, the content width option in the Layout Element.
What you can do is make your code a little more bulletproof.
So instead of just
global $post;
, do this:global $post; if ( ! is_object( $post ) ) { return $display; }
Let me know if that helps or not π
January 27, 2021 at 5:01 am #1635401NatalieThanks Tom
That is obviously more robust than using
if(is_admin()) return true;
and it works fine
How are you getting on with a fix for the header population issue? (which is why I need this code)
ref: https://generatepress.com/forums/topic/element-display-rules-very-slow-to-populate/January 27, 2021 at 9:39 am #1635954TomLead DeveloperLead DeveloperThat’s a difficult issue to fix, but it’s one that’s still on my radar.
We’re making some big changes to Elements, and I suspect a Display Rules re-write will be in phase 2 π
January 28, 2021 at 12:56 am #1636531Nataliegreat – thanks for the update and the excellent support as always π
January 28, 2021 at 9:36 am #1637294TomLead DeveloperLead DeveloperNo problem! π
-
AuthorPosts
- You must be logged in to reply to this topic.