获取 CPT 页面的顶级父类别名称和 URL

问题描述

我在我的functions.PHP 中使用以下代码获取CPT 页面父类别。

// Get Parent Category Link
function get_parent_category () {

      $terms = get_the_terms($post->ID,'section' );
      if ($terms && ! is_wp_error($terms)) :
          $tslugs_arr = array();
          foreach ($terms as $term) {
              $tslugs_arr[] = $term->slug;
          }
          $terms_slug_str = join( " ",$tslugs_arr);
      endif;

      $categories = get_the_terms( $post->ID,'section' );

      {
      foreach ( $terms as $term ) {
          $term_link = get_term_link( $term );
          if ( is_wp_error( $term_link ) ) {
              continue;
          }
      return   esc_url( $term_link ) ;
      // echo   $term->name  ;
      }

      }

}

// Get Parent Category Name
function get_parent_category_name () {

      $terms = get_the_terms($post->ID,'section' );

      {
      foreach ( $terms as $term ) {
          $term_link = get_term_link( $term );
          if ( is_wp_error( $term_link ) ) {
              continue;
          }
      //return   esc_url( $term_link ) ;
    return   $term->name  ;
      }

      }

}

我还需要获取同一页面的顶级类别名称和 url,我无法弄清楚如何获取顶级类别并需要它以便我可以在自定义面包屑路径中显示

然后我使用以下代码在我的主题模板中显示父类别。

    $crumbs[3]['label'] = get_parent_category_name ();
    $crumbs[3]['url'] = get_parent_category () ;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)