我有一个这样的导航结构:
如果我查看的是有关页面,我希望侧边栏显示:
如果我查看的是“服务”页面,我希望侧边栏显示:
这是我目前使用的代码。我如何修改它,使它以我想要的方式工作?
<?php
global $wp_query;
if( empty($wp_query->post->post_parent) ) {
$parent = $wp_query->post->ID;
} else {
$parent = $wp_query->post->post_parent;
}
$children = get_pages('child_of='.$parent);
if( count( $children ) > 0 ) {
?>
<ul>
<?php wp_list_pages ("&title_li=&child_of=$parent"); ?>
</ul>
<?php } ?>发布于 2013-08-23 01:08:51
这要么直接做到,要么至少向您展示如何:
当查看有子页(或其子页)时,它只显示该父页的子页。
<?php
$output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
if (is_page( )) {
$page = $post->ID;
if ($post->post_parent) {
$page = $post->post_parent;
}
$children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
if ($children) {
$output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
}
}
echo $output;
?>来自:subpage
此方法利用了echo=0参数,该参数将结果作为字符串返回给变量。if ($children)测试现在起作用了,因为如果没有找到匹配项,wp_list_pages()将返回空值。
https://stackoverflow.com/questions/18387418
复制相似问题