Undefined property: WP_Error::$cat_ID

Viewed 87

I'm having difficulties after updating the site's php, wordpress is returning error.

Before he informede rro Warning: Declaration of my_Walker_CategoryDropdown::start_el(&$output, $category, $depth, $args) should be compatible with Walker_CategoryDropdown::start_el(&$output, $category, $depth = 0, $args = Array, $id = 0)

adjusted to: class my_Walker_CategoryDropdown extends Walker_CategoryDropdown {

  function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0) {
    $pad = str_repeat(' ', $depth * 3);

after the adjustment, the error disappeared and another one appeared now

Warning: Undefined property: WP_Error::$cat_ID on line 174

the code on that line is like this

add_filter('admin_footer_text', 'remove_footer_admin');
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$url = get_category_link( $cat_id );
echo $url;

someone help me please

1 Answers

This means, that if the query_var('cat') doesn't exist, you will get an error for $cat_id since you have an undefined variable $category - It's not a fatal error, just a warning, but could easily be avoided.

This instead checks if $category is defined:

add_filter( 'admin_footer_text', 'remove_footer_admin' );
function remove_footer_admin() {
    $category = get_category( get_query_var( 'cat' ) );
    if ( $category ) {
        $cat_id = $category->cat_ID;
        $url    = get_category_link( $cat_id );
        echo esc_url( $url );
    }
}
Related