如何将空父类别重定向到它在 wordpress 中的第一个非空子类别

how to redirect empty parent category to its first non empty child category in wordpress

在 Wordpress 中,如果父类别为空,那么它将重定向到其第一个非空的子类别。

试试这个

$category = get_queried_object();
    $count_post = $category->count;

    if($count_post==0)
    {
       $args=array(
                'child_of' => $cat-id,
                'hide_empty' => 1,
                'orderby' => 'id',
                'order' => 'ASC',
                'depth' => '1' 

            );
            $categories=get_categories($args);
            $cat_link;
            foreach($categories as $category) {
                  $cat_link=get_category_link( $category->term_id );
                break;
            }
               wp_redirect( $cat_link ); 
    }