我正在使用下面的代码添加一个子菜单,该子菜单只显示当前的顶级父级和父级中的任何子页面。它在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 );
}发布于 2013-09-20 16:22:27
我需要将global $post;添加到函数中。它起作用了!
https://stackoverflow.com/questions/18920609
复制相似问题