【问题标题】:Wordpress navigation for grandchildren孙子的WordPress导航
【发布时间】:2012-06-28 18:37:24
【问题描述】:

我目前正在使用此代码(根据法典)在父页面上显示子页面,并在其子页面上显示父页面的子页面:

<?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) { ?>
    <ul>
    <?php echo $children; ?>
    </ul>
<?php } ?>

我想补充一点,如果在次要子页面(孩子的孩子)上,则显示它的父母和父母的兄弟姐妹。

感谢您的帮助! :D

【问题讨论】:

    标签: php wordpress templates wordpress-theming


    【解决方案1】:
    <?php
    if($post->post_parent)
    {
        //get the parent post
        $parent = get_post($post->post_parent);
        //check to see if we have a grandparent
        if($parent->post_parent)
        {
            $page_list = wp_list_pages( array( 'child_of' => $parent->post_parent, 'echo' => false, 'depth' => 1 ) );   
        }
        else
        {
            $page_list = wp_list_pages( array( 'child_of' => $post->post_parent, 'echo' => false, 'depth' => 1 ) );
        }
    }
    else
         $page_list = wp_list_pages( array( 'child_of' => $post->ID, 'echo' => false, 'depth' => 1 ) );
    if ($page_list) { 
    ?>
    <ul>
    <?php echo $page_list; ?>
    </ul>
    <?php } ?>
    

    这将检查帖子是否有父级,然后检查该帖子是否有父级。 $page_list 应该是父页面及其兄弟页面的列表。 'depth' =&gt; 1 告诉 WordPress 只获取一级页面。这将阻止它获取这些页面的子页面

    【讨论】:

    • 嗯,这如何与我已有的代码一起使用?我仍然需要父页面和子页面的代码..你能给我看一个例子吗?
    • 太棒了!非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多