【问题标题】:Show submenu for a particular parent level item in WordPress在 WordPress 中显示特定父级项目的子菜单
【发布时间】:2011-04-27 00:19:44
【问题描述】:

我在 wordpress 网站上有一个使用 wp_nav_menu 的主菜单,它作为 4 个父项,每个都有几个子菜单项。

在特定模板上,我想显示其中一个父项的子菜单项。例如:

  • 父项 1
    • 列表项 1
    • 列表项 2
  • 父项 2
    • 列表项 1
    • 列表项 2
  • 父项 3
    • 列表项 1
    • 列表项 2
  • 父项 4
    • 列表项 1
    • 列表项 2

在我的模板中,我想为父项 3 显示列表项 1 和列表项 2。每个父项都有一个唯一的 ID。在“页面”上,我使用以下代码来完成它:

            <?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 id="side-page-menu">
                    <?php echo $children; ?>
                </ul>
        <?php } ?>

但这似乎不适用于单个帖子(与页面相比)。

谢谢!

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    试过了

    <ul>
                <?php wp_list_pages('title_li=<h2>MAIN NAV</h2>&depth=1' ); ?>
                </ul>
                <?php
                    if ($post->post_parent == 0) {
                    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
                    $parentpage = $wpdb->get_row("SELECT ID, post_title, post_name FROM $wpdb->posts WHERE ID = '".$post->ID."'");
                    }
                    if ($post->post_parent != 0) {
                    $next_post_parent = $post->post_parent;
                    while ($next_post_parent != 0) {
                    $children = wp_list_pages("title_li=&child_of=".$next_post_parent."&echo=0");
                    $parentpage = $wpdb->get_row("SELECT ID, post_title, post_parent, post_name FROM $wpdb->posts WHERE ID = '".$next_post_parent."'");
                    $next_post_parent = $parentpage->post_parent;
                    }
                }
                ?>
                <?php if ($children) { ?>
                <ul>
                    <li>SUBNAV FOR: <a href="<?php echo get_permalink($parentpage->ID); ?>"><?php echo $parentpage->post_title; ?></a>
                <ul>
                <?php echo $children; ?>
                </ul>
                </li>
                </ul>
                <?php } ?>
    

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 2013-02-23
      • 2013-03-14
      • 2017-09-30
      • 2014-07-25
      • 1970-01-01
      相关资源
      最近更新 更多