当前位置: 首页 > article >正文

wordpress子分类调用父分类名称和链接的3种方法

专为导航而生,在wordpress模板制作过程中常常会在做breadcrumbs导航时会用到,子分类调用父分类的名称和链接,下面这段简洁的代码,可以完美解决这个问题。

<?php echo get_category_parents( $cat, true, ' &raquo; ' ); ?>

下面这种方法也可以,不过代码不够简洁。

<?php
if ( is_category() ) {
    // Get the current category term id.
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
 
    echo get_term_parents_list( $term_id, 'category' );
}
?>

第三种方法,调用分类目录名称和链接,作为导航。

<?php
if ( ( is_tax() || is_category() || is_tag() ) ) {
    $trail     = '';
    $home      = '/<a href="' . get_home_url() . '">Home</a>';
    $query_obj = get_queried_object();
    $term_id   = $query_obj->term_id;
    $taxonomy  = get_taxonomy( $query_obj->taxonomy );
 
    if ( $term_id && $taxonomy ) {
        // Add taxonomy label name to the trail.
       // $trail .=  '/' . $taxonomy->labels->menu_name;
        // Add term parents to the trail.
        $trail .= '/' . get_term_parents_list( $term_id, $taxonomy->name, array( 'inclusive' => false ) );
    }
 
    // Print trail and add current term name at the end.
    echo '<p class="breadcrumb-trail">' . $home . $trail . $query_obj->name . '</p>';
}
?>

 原文

http://www.chudafu.com/jianzhan/4409.html


http://www.kler.cn/a/571964.html

相关文章:

  • Android 系统开发的指导文档
  • 奇安信天擎面试题
  • 未来经济范式争夺战:AR眼镜为何成为下一代交互终端的制高点?
  • 【TCP/IP协议栈】2. 网络接口层协议(以太网、令牌环、PPP 等)
  • 【STM32】TIM输入捕获-学习笔记
  • 2025年能源工作指导意见
  • 【Unity】鼠标在某区域悬停触发文本框,移开关闭文本框
  • 蔡司智锐系列眼镜:智能动态护眼,畅享数字化生活
  • doris: Hive
  • 通俗的方式解释“零钱兑换”问题
  • mysql空间占用
  • Git基本命令索引
  • 【音视频】音频基础
  • SQLite Alter 命令详解
  • android13打基础:控件datepicker
  • 【每日学点HarmonyOS Next知识】手写电子签名、瀑布流布局获取字符串高度、WaterFlow崩溃、Web滑动问题
  • 记一次误禁用USB导致键盘鼠标失灵的修复过程
  • BambuStudio学习笔记:Geometry类
  • Linux系列:如何用 C#调用 C方法造成内存泄露
  • 计算机毕业设计SpringBoot+Vue.js网上服装商城(源码+文档+PPT+讲解)