次のコードを使用して、現在の最上位の親ページと、親内にある場合は子ページのみを表示するサブメニューを追加しています。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 );
}