我一直试图在Wordpress中设置一个基本的侧边栏,它有几个条件。
我已经获得了一些结果,但它添加的页面不是直接的孩子。
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<?php echo '<h4>Explore ' . get_the_title($parent[1]) . '</h4>'; ?>
<?php echo $children; ?>
答案 0 :(得分:0)
这个问题分为两部分
depth
传递给wp_list_pages()
,以指定层次结构级别。以下代码未经测试,但逻辑应该是正确的:
if($post->post_parent){
// get a list of all the children of the parent page
$pages = get_pages(array('child_of'=>$post->post_parent));
if ($pages) {
// get the ids for the pages in a comma-delimited string
foreach ($pages as $page)
$page_ids[] = $page->ID;
$siblings = implode(',',$page_ids);
// $pages_to_get is a string with all the ids we want to get, i.e. parent & siblings
$pages_to_get = $post->post_parent.','.$siblings;
// use "include" to get only the pages in our $pages_to_get
$children = wp_list_pages("include=".$pages_to_get."&echo=0");
}
}
else{
// get pages that direct children of this page: depth=1
$children = wp_list_pages("title_li=&child_of=".$post->ID."&depth=1&echo=0");
}
// display the children:
if ($children) {
echo '<h4>Explore ' . get_the_title($parent[1]) . '</h4>';
echo $children;
}
?>