【问题标题】:Wordpress - Showing custom menu links while showing child pagesWordpress - 在显示子页面时显示自定义菜单链接
【发布时间】:2020-08-08 13:27:55
【问题描述】:

我有一个简单地显示当前页面的所有子页面的循环:

<?php 
  $args = array(
        'parent' => $post->ID,
        'post_type' => 'page',
        'sort_order' => 'ASC'
  ); 
  $pages = get_pages($args);  ?>

<?php foreach( $pages as $page ) { ?>

        <div>
            <p><?php echo $page->post_title; ?></p>
        </div>

<?php } ?>

此页面的导航如下所示:

Parent Page
 - Child page
 - Child page
 - Child page
 - Custom Link (added in appearance > menus) 
 - Custom link (added in appearance > menus)
 - Page which has another parent (added in appearance > menus)

上面的代码正确显示了所有直接子页面,但我希望它显示自定义链接和我添加到菜单下拉菜单中的其他页面。

我尝试使用wp_get_nav_menu_items 代替get_pages 并使用'post_type' =&gt; 'page',但我似乎无法正常工作。我可以显示所有页面的完整列表,也可以只显示直接子页面。

谁能告诉我哪里出错了?我觉得这应该是一件很容易的事情......

【问题讨论】:

  • 这与你之前的问题几乎是重复的,Wordpress Loop - Show children menu dropdown instead of direct page children 你应该在那里继续讨论,而不是创建一个新问题。
  • 不是真正的 CBroe,那是我的帖子,我相信您也知道,因为您对此发表了评论。我试图简化问题并再次发布。虽然您的评论很有帮助并且我很感激,但我缺乏进一步了解它的知识,所以我仍然卡住了。
  • 老实说,如果您能进一步解释,将会很有帮助...如果您真的想帮忙?
  • 我确实在那里继续谈话,但你没有回复,所以我简化了这个问题,因为我认为它可能一开始就太复杂了。
  • 此时您不能期望通过查询页面对象来获得您的自定义链接——它们一开始就不是页面。使用 WP 的功能来呈现导航菜单真正你应该在这里使用。从那开始 - 使用wp_nav_menu 以标准方式输出此菜单,这在 WP 中有效。作为第一步,这应该会为您提供所有所需的链接。然后,您可以通过定义自定义导航步行器来查看修改此创建的 HTML 输出以满足您的需求。

标签: php wordpress loops wp-nav-menu-item


【解决方案1】:

好的,我找到了一种方法,可以使用 functions.php 文件中的自定义 walker 类来使其工作,如下所示:

class Selective_Walker extends Walker_Nav_Menu
{
    function walk( $elements, $max_depth) {

        $args = array_slice(func_get_args(), 2);
        $output = '';

        if ($max_depth < -1) //invalid parameter
            return $output;

        if (empty($elements)) //nothing to walk
            return $output;

        $id_field = $this->db_fields['id'];
        $parent_field = $this->db_fields['parent'];

        // flat display
        if ( -1 == $max_depth ) {
            $empty_array = array();
            foreach ( $elements as $e )
                $this->display_element( $e, $empty_array, 1, 0, $args, $output );
            return $output;
        }

        /*
         * need to display in hierarchical order
         * separate elements into two buckets: top level and children elements
         * children_elements is two dimensional array, eg.
         * children_elements[10][] contains all sub-elements whose parent is 10.
         */
        $top_level_elements = array();
        $children_elements  = array();
        foreach ( $elements as $e) {
            if ( 0 == $e->$parent_field )
                $top_level_elements[] = $e;
            else
                $children_elements[ $e->$parent_field ][] = $e;
        }

        /*
         * when none of the elements is top level
         * assume the first one must be root of the sub elements
         */
        if ( empty($top_level_elements) ) {

            $first = array_slice( $elements, 0, 1 );
            $root = $first[0];

            $top_level_elements = array();
            $children_elements  = array();
            foreach ( $elements as $e) {
                if ( $root->$parent_field == $e->$parent_field )
                    $top_level_elements[] = $e;
                else
                    $children_elements[ $e->$parent_field ][] = $e;
            }
        }

        $current_element_markers = array( 'current-menu-item', 'current-menu-parent', 'current-menu-ancestor' );  //added by continent7
        foreach ( $top_level_elements as $e ){  //changed by continent7
            // descend only on current tree
            $descend_test = array_intersect( $current_element_markers, $e->classes );
            if ( !empty( $descend_test ) ) 
                $this->display_element( $e, $children_elements, 2, 0, $args, $output );
        }

        /*
         * if we are displaying all levels, and remaining children_elements is not empty,
         * then we got orphans, which should be displayed regardless
         */
         /* removed by continent7
        if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
            $empty_array = array();
            foreach ( $children_elements as $orphans )
                foreach( $orphans as $op )
                    $this->display_element( $op, $empty_array, 1, 0, $args, $output );
         }
        */
         return $output;
    }
}

然后使用这个到我的模板页面来简单的菜单:

<?php

$menuParameters = array(
    'theme_location'    =>'primary',
    'walker'=>new Selective_Walker()
);

echo wp_nav_menu( $menuParameters );

?>

这有助于显示当前页面标题以及所有子导航项,而不仅仅是页面项。

【讨论】:

    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多