【问题标题】:How to hide Top Parent Pages in Wordpress navigation如何在 Wordpress 导航中隐藏顶级父页面
【发布时间】:2016-10-11 19:07:35
【问题描述】:

我的页面上有两个导航栏。一个是主要的,标题,第二个显示在每个页面的顶部。

它的外观如下:

下部导航栏应在顶部导航栏(红色)中显示页面的子页面。 如果下部导航栏中的某个页面有一个子页面,它将在下部导航栏下方显示为新的 ul。

现在的问题是,如果我在“主要”页面之一(顶部链接的那个)上,那么顶部导航栏中的链接将显示在下部导航栏中,并且应该显示的链接显示在下方导航栏下方的 ul 中。

像这样(font-color:red中的链接是children的children)

我明白为什么会发生这种情况,我只是不知道如何解决这个问题。如果我不在顶级父页面上,我只想显示兄弟姐妹。

好的,所以试着让它更清楚一点: 如果在顶部父页面上,则显示子级。 如果在儿童页面上,请显示兄弟姐妹和孙子。 如果在孙子页面中,请显示兄弟姐妹和父母。

这是我的代码:

    <?php

$sibblings = wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->post_parent,
    'echo'     => 0,
    'sort_column' => 'menu_order, post_title'
) );

$children = wp_list_pages( array(
    'title_li' => '',
    'child_of' => $post->ID,
    'echo'     => 0,
    'sort_column' => 'menu_order, post_title'
) );




?>

    <ul class="page-nav hidden-sm hidden-xs">


        <?php echo $sibblings; ?>
</ul>


<ul class="page-nav-children hidden-sm hidden-xs">
    <?php echo $children; ?>
</ul>

更新

好的,所以我尝试了一些新的东西。现在不显示顶级父母,但是当点击带有孩子(顶级父母孙子)的页面时,他们不会显示。

$childrenvar_dump 上返回为空

新代码:

    <?php

    if ($post->post_parent) {
        $ancestors=get_post_ancestors($post->ID);
        $root=count($ancestors)-1;
        $parent = $ancestors[$root];
    } else {
        $parent = $post->ID;
    }

    $sibblings = wp_list_pages("title_li=&child_of=". $parent ."&echo=0");

    $children = wp_list_pages( array(
        'title_li' => '',
        'child_of' => $post->ID,
        'echo'     => 0,
        'sort_column' => 'menu_order, post_title'
    ) );

    ?>

    <?php if ($sibblings) { ?>
    <ul class="page-nav hidden-sm hidden-xs">
        <?php echo $sibblings; ?>
    </ul>
<?php } else {?>
    <ul class="page-nav hidden-sm hidden-xs">
        <?php echo $sibblings; ?>
    </ul>
    <ul class="page-nav-children hidden-sm hidden-xs">
        <?php echo $children; ?>
    </ul>

<?php } ?>

【问题讨论】:

    标签: html wordpress navigation navbar


    【解决方案1】:

    您可以查看 $sibblings 数组中的任何对象是否与当前帖子具有相同的 post_title。如果是,请使用 unset 将其从数组中删除。这样它就不会显示在导航中。

        $current_post = get_post();
        $sibblings = wp_list_pages( array(
        'title_li' => '',
        'child_of' => $post->post_parent,
        'echo'     => 0,
        'sort_column' => 'menu_order, post_title'
        ) );
    
        foreach ($sibblings as $key => $sibbling) {
            if ($sibbling->post_title != $current_post->post_title) {
                unset($sibblings[$key]);
            }
        }
    

    下面是如果您不想使用 unset 而是使用数组推送将所有不是当前帖子的帖子推送到新数组中。

        $current_post = get_post();
        $sibblings = wp_list_pages( array(
        'title_li' => '',
        'child_of' => $post->post_parent,
        'echo'     => 0,
        'sort_column' => 'menu_order, post_title'
        ) );
        $sibblings_only_array = array();
        foreach ($sibblings as $sibbling) {
            if ($sibbling->post_title != $current_post->post_title) {
                array_push($sibblings_only_array, $sibbling);
            }
        }
    

    如果上面的方法不起作用,那么添加你的 sibblings 数组转储出来,这样我就可以准确地看到那里的数据。谢谢!

    【讨论】:

    • 但不会被删除的标题相同的帖子。它是所有页面的顶部父页面,如最顶部红色标题中的五个链接。它们永远不会像现在那样显示在灰色区域中。
    • 好的,所以试着让它更清楚一点:如果在顶部父页面上,显示孩子。如果在儿童页面上,请显示兄弟姐妹和孙子。如果在孙子页面中,请显示兄弟姐妹和父母。从不显示最高父级。
    • 转储您的兄弟数组并将其添加到您的代码中。
    • 我把它作为一个包含所有页面的列表。尝试了var_dumpprint_r
    • 所以它是一个字符串?无论如何,我认为您需要在这里向任何人展示这些变量所涉及的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 2019-04-01
    • 2011-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多