Wordpress 为什么赢了';Twp_list_页面是否在functions.php中工作?

Wordpress 为什么赢了';Twp_list_页面是否在functions.php中工作?,wordpress,Wordpress,我正在使用下面的代码添加一个子菜单,该子菜单仅显示当前顶级父级页面以及父级页面中的所有子级页面。当直接在sidebar.php中使用时,它可以工作,但是当我将它包装到一个函数中并将其放置在functions.php中时,它只显示所有顶级菜单项,而不显示活动的父菜单项。我怎样才能让它工作 在侧边栏中工作: <?php $child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );

我正在使用下面的代码添加一个子菜单,该子菜单仅显示当前顶级父级页面以及父级页面中的所有子级页面。当直接在sidebar.php中使用时,它可以工作,但是当我将它包装到一个函数中并将其放置在functions.php中时,它只显示所有顶级菜单项,而不显示活动的父菜单项。我怎样才能让它工作

在侧边栏中工作:

<?php
$child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
    $depth_value = ( $post->post_parent ? 2 : 1 );
        $wp_list_pages_args = array( 
            'child_of' => $child_of_value,
            'depth' => $depth_value,
            'title_li' => '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'
        );
    wp_list_pages( $wp_list_pages_args );
?>

在functions.php中不起作用

function page_submenu() {   
    $child_of_value = ( $post->post_parent ? $post->post_parent : $post->ID );
    $depth_value = ( $post->post_parent ? 2 : 1 );
        $wp_list_pages_args = array( 
            'child_of' => $child_of_value,
            'depth' => $depth_value,
            'title_li' => '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'
        );
    wp_list_pages( $wp_list_pages_args );
}
函数页_子菜单(){
$child\u of_value=($post->post\u parent?$post->post\u parent:$post->ID);
$depth\u value=($post->post\u父项?2:1);
$wp\u list\u pages\u args=数组(
'child_of'=>$child_of_value,
“深度”=>$depth\u值,
“title_li'=>”
);
wp_列表_页面($wp_列表_页面_参数);
}

我需要添加
global$post到函数。它起作用了