我在functions.php
文件:
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 : "";
}
}
}
但我一直在收到PHP警告:
PHP注意:试图在/var/www/vhosts/elclarinweb中获取非对象的属性。com/httpdocs/sistema/wp-content/themes/elclarin\\u v2/functions。php第86行
我如何解决这个小问题?
最合适的回答,由SO网友:TheDeadMedic 整理而成
get_object_vars()
返回对象属性的关联数组-soqueried_object
从不在中$wpQueryObj
, 这是一把钥匙:$wpQueryObj[\'queried_object\']
.
除此之外,你无论如何都不应该这样做-使用get_queried_object()
:
if ( $wp_query instanceof WP_Query ) {
if ( $object = $wp_query->get_queried_object() ) {
$query_name = isset( $object->name ) ? $object->name : \'\';
}
}