Archived topic
Trying to add different logos for different pages using parse_query
7 replies · Started by Matt on July 23, 2020
Hi there,
I am building a site on my localhost and I am trying to add in different logos to different pages. I am using code that I got from the Generatepress support forums and it is working, however I am getting an error at the top of the home page and only the home page, when using wp_debug. The message is not visible with wp_debug disabled.
The error is:
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3994
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3996
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3998
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3994
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3996
Notice: Trying to get property of non-object in XXXXX/wp-includes/class-wp-query.php on line 3998
I'm pretty sure it is due to me using 'is_front_page()' and 'is_page(89)' but I can't work out why I only get the error on the home page. It is working fine and there is no error message on the page: is_page(89).
When I added in the second if statement in the align_add_logo_filters() function it doubled the error output.
The code I am including at the top of my functions.php is:
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/
include('includes/partials.php');
include('includes/hooks-n-filters.php');
The code in my 'hooks-n-filters.php' file is:
<?php
global $post;
//Swap Navigation logo on homepage for white one.
//Build the filter but it will not work by checking the homepage as it needs to be parsed first
if ( ! function_exists( 'align_change_navigation_logo_output' ) ) {
function align_change_navigation_logo_output( $output ) {
return sprintf(
'<!--standard generatepress logo code-->',
esc_url( 'Image_URL' ),
esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) )
);
}
}
if ( ! function_exists( 'align_change_navigation_logo_output_music_change' ) ) {
function align_change_navigation_logo_output_music_change( $output ) {
return sprintf(
'<!--standard generatepress logo code-->',
esc_url( 'Image_URL' ),
esc_attr( apply_filters( 'generate_logo_title', get_bloginfo( 'name', 'display' ) ) ),
esc_url( apply_filters( 'generate_logo_href' , home_url( '/' ) ) )
);
}
}
//add action to parse
add_action( 'parse_query', 'align_add_logo_filters' );
function align_add_logo_filters() {
if ( is_front_page() ) {
add_filter( 'generate_navigation_logo_output', 'align_change_navigation_logo_output' );
} else if ( is_page( 89 ) ) {
add_filter( 'generate_navigation_logo_output', 'align_change_navigation_logo_output_music_change' );
}
}
Also if there is a smart way to combine the two filters 'align_change_navigation_logo_output' and 'align_change_navigation_logo_output_music_change' I would appreciate any hints/tips.
BTW awesome theme I use it a lot.
Cheers
Hi there,
can you provide a link to the site so we can see what the issue is?
You can edit your original topic and use the Site URL field to share the link privately.
Hi, thanks for getting back to me so quickly!
I have uploaded it to a subdomain and shared the url with you. Interestingly I notice the error message has changed now it is not on my mamp localhost:
Notice: Trying to get property 'ID' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3994
Notice: Trying to get property 'post_title' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3996
Notice: Trying to get property 'post_name' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3998
Notice: Trying to get property 'ID' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3994
Notice: Trying to get property 'post_title' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3996
Notice: Trying to get property 'post_name' of non-object in XXXXX/wp-includes/class-wp-query.php on line 3998
Hi there,
Those notices would be coming from code that looks like this:
$post->post_title
$post->ID
Basically, it's saying you're trying to get a property (->post_title) on a variable that isn't an object ($post). $post is an object by default, but may not be called correctly in your code somewhere.
I'm not seeing anything in the code you've shared that would output these notices.
yeh, I'm really not sure why it is throwing the errors.
The code is working as expected on all pages including the homepage but it is only throwing the errors on the homepage. If I switch off wp_debug the site works perfectly but it is annoying knowing that there is that error. I have used the same code on another site in exactly the same way and didn't receive an error.
The error disappears if I remove the if_page() statements which correlates with it being 'class-wp-query.php on line 3994'.
To be honest it's not even that complicated a site which is what is making it super frustrating.
Also do you know why it added in 'get property ‘post_title’' when i uploaded it to a sub domain but when it was on my localhost it said 'get property of non-object '?
What if you use the wp hook instead of parse_query?
Wonder if the hook itself is causing the issue.
Yes, that was it! I changed the hook like you suggested and the error disappeared.
Thanks so much for taking the time to look at this problem, I super appreciate it:)
Glad I could help! :)