duanhuiqing9528 2014-01-08 13:08
浏览 140
已采纳

Wordpress:显示父菜单标题

I want to show the parent menu item above the submenu but when there is no submenu the parent menu item must not be showing then.

Now i use this code (page.php):

<div class="left">      
    <h2>
        <?php
            $parent_title = get_the_title($post->post_parent);
            echo $parent_title;
        ?>
    </h2>

    <?php wp_nav_menu( array('container_class' => 'Hoofdmenu','theme_location' => 'Hoofdmenu','sub_menu' => true) ); ?>

    <?php get_sidebar(); ?>
</div>

Output:

TITLE PARENT MENU
Subitem
Subitem
Subitem
------------
Widgets and stuff not important.

But with this code the parent menu item (title) is always visible even when there is no submenu...

  • 写回答

1条回答 默认 最新

  • duanlongnao0028 2014-01-08 14:01
    关注

    Looks like what you need is a combination of checking if the page is a parent or not and then also seeing if it has children.

    Something like this should work.

    <?php
    
    if ( is_page($post->ID) && $post->post_parent ) {
      $children = get_pages('child_of='.$post->post_parent);
    } else {
      $children = get_pages('child_of='.$post->ID);
    }
    
    ?>
    
    <?php if($children):?>
      <h2>
        <?php 
        $parent_title = get_the_title($post->post_parent);
        echo $parent_title;
        ?>
      </h2>
    <?php endif;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题