0

I've this function at functions.php file:

function custom_secondary_nav( $menu_name, $id = '', $menu_title = '' )
{
    global $wp_query;

    // others stuff goes here   

    if (is_object($wp_query)) {
        $wpQueryObj = get_object_vars($wp_query);
        if (in_array("queried_object", $wpQueryObj)) {
            $query_name =  $wp_query->queried_object->name !== "" ? $wp_query->queried_object->name : "";
        } 
    }
}

But all the time I'm getting this PHP Warning:

PHP Notice: Trying to get property of non-object in /var/www/vhosts/elclarinweb.com/httpdocs/sistema/wp-content/themes/elclarin_v2/functions.php on line 86

How I can fix that small issue?

1 Answer 1

1

get_object_vars() returns an associative array of object properties - so queried_object is never in $wpQueryObj, it's a key: $wpQueryObj['queried_object'].

Besides, you shouldn't be doing any of that anyway - use get_queried_object():

if ( $wp_query instanceof WP_Query ) {
    if ( $object = $wp_query->get_queried_object() ) {
         $query_name = isset( $object->name ) ? $object->name : '';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.