在儿童菜单和孙子菜单wordpress上显示面包屑

时间:2016-09-07 09:29:16

标签: php wordpress

所以我有一个这样的菜单:

-inspiration<parent page>
 -- healt<child page>
  --Medicine<grandchild page>
  --Teraphy<grandchild page>
 -- Mind<child page>
  --Consult<grandchild page>
  --Psych<grandchild page>

我只想在打开子页面和大子页面时显示面包屑。

例如,当我打开“inpiration”页面时,我不想显示任何面包屑。 但是当我打开“健康”页面时,我想要输出带有“健康”和“药物”输出的面包屑。

base.php我有这样的代码:

<div class="second-menu">
  <div class="container">
    <ul>
      <?php

      $args = array(
      'child_of' => get_top_ancestor_id(),
      'title_li' => ''
      );

     ?>

      <?php wp_list_pages($args); ?>
    </ul>
  </div>
</div>

在我的function.php我有这样的代码:

function get_top_ancestor_id() {

    global $post;

    if ($post->post_parent) {
        $ancestors = array_reverse(get_post_ancestors($post->ID));
        return $ancestors[0];

    }

    return $post->ID;

}

但是在我刷新子页面(“healt”页面)之后,面包屑的输出是这样的:

-healt
--Medicine
--Teraphy
-Mind
--Consult
--Psych

我想要的只是:

--Medicine
--Teraphy

我认为因为“健康”页面被视为子页面。请帮忙。

1 个答案:

答案 0 :(得分:0)

功能

wp_list_pages

将仅按ID显示“单页的子页面”,以便在“健康页面”中显示此功能

$ancestors = array_reverse(get_post_ancestors($post->ID));

将返回“灵感”的孩子,因为您正在返回“医学”父母页面ID。也许你只能尝试打印孩子,比如

$result = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM wp_posts WHERE post_parent = %s AND post_type = %s", $post->ID, 'page' ) );
if ( $result ) {
    foreach ( $result as $page ) {
        array_push( $childs, $page->ID );
    }
}

类似的东西。