You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取仅父级/子级页面?PHP菜单功能问题求助

问题:WordPress菜单中父级页面无子女时需可点击的实现问题

我正在开发一段WordPress相关的PHP代码,目的是从菜单中分别获取父级页面和子级页面。需求是当父级页面没有子级页面时,该父级页面需要可点击。我已经给无子级的父级页面传入了postid和taxonomy参数,但问题仍未解决,相关代码如下:

//Get parent terms only
function bdb_get_parent_terms($postid=null,$taxonomy){
  if ($postid === null)
  return false;

  $terms=wp_get_post_terms($postid,$taxonomy);
  $termr = null;

  if($terms){
    foreach ( $terms as $term ) {
        if($term->parent=='0') $termr[]=$term;
    }
    return $termr;
  }
  return false;
}

//Get parent terms only
function bdb_get_father_terms($postid=null,$taxonomy,$type){
  if ($postid === null)
  return false;

  $termsFather=wp_get_post_terms($postid,$taxonomy);

  if($termsFather){
    foreach ( $termsFather as $termFather ) {
        $termsFatherArr[]=$termsFather;
    }
    return $termsFatherArr[0][1]->$type;
  }
  return false;
}

//Get child terms only
function bdb_get_child_terms($postid=null,$taxonomy){
  if ($postid === null)
  return false;

  $terms=wp_get_post_terms($postid,$taxonomy);
  $termr = null;

  if($terms){
    foreach ( $terms as $term ) {
        if($term->parent!='0') $termr[]=$term;
    }
    return $termr;
  }
  return false;
}

// Check if has children page
function bdb_has_children($postid) {
  
  global $post;
  $pages = get_children($postid);
  
  if (!empty($pages)):
    return true;
  else:
    return false;
  endif;
}

问题分析与修正方案

1. 核心问题:bdb_has_children函数逻辑错误

get_children()需要传入参数数组,直接传$postid不会生效,且无需全局$post变量。修正后:

// 检查页面是否有子页面
function bdb_has_children($postid) {
    $child_pages = get_children(array(
        'post_parent' => $postid,
        'post_type'   => 'page',
        'numberposts' => 1,
        'post_status' => 'publish'
    ));
    return !empty($child_pages);
}

2. bdb_get_father_terms函数逻辑混乱

该函数命名与实现不符,循环中错误重复存入数组,硬取索引的方式不可靠。如果需求是获取页面所属术语的父级术语,重写如下:

// 获取页面所属术语的父级术语属性
function bdb_get_parent_term($postid=null,$taxonomy,$type){
    if ($postid === null) return false;

    $terms = wp_get_post_terms($postid,$taxonomy);
    if(empty($terms)) return false;

    $term = $terms[0];
    if($term->parent != 0){
        $parent_term = get_term($term->parent, $taxonomy);
        return $parent_term->$type ?? false;
    }
    return $term->$type ?? false;
}

3. 页面与术语的概念区分

代码同时处理页面父子关系和分类术语父子关系,需明确菜单类型:

  • 若为页面菜单:用修正后的bdb_has_children判断页面子项
  • 若为分类菜单:用以下函数判断术语子项
// 检查分类术语是否有子术语
function bdb_term_has_children($term_id, $taxonomy) {
    $child_terms = get_term_children($term_id, $taxonomy);
    return !empty($child_terms);
}

4. 菜单实现“无子级则可点击”示例

在菜单模板中使用修正后的函数:

// 假设当前父页面ID为$parent_id
if(bdb_has_children($parent_id)){
    // 有子页面,父级作为菜单标题
    echo '<li class="menu-parent">' . get_the_title($parent_id) . '</li>';
    // 渲染子页面列表...
} else {
    // 无子页面,父级作为可点击链接
    echo '<li class="menu-parent"><a href="' . get_permalink($parent_id) . '">' . get_the_title($parent_id) . '</a></li>';
}

内容的提问来源于stack exchange,提问作者Karina Mercedes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 20:03:16