wordpres当前分类调用父分类的名称和链接

在WordPress中,如果你想在当前分类页面调用并显示父分类的名称和链接,你可以使用以下代码片段:

复制代码
<?php
// 获取当前分类的ID
$cat_id = get_queried_object_id();

// 获取当前分类的父分类ID
$parent_id = get_term($cat_id, 'category')->parent;

// 如果存在父分类,则获取其名称和链接
if ($parent_id) {
    $parent_cat = get_term($parent_id, 'category');
    $parent_name = $parent_cat->name;
    $parent_link = get_category_link($parent_id);
    echo '<a href="' . esc_url($parent_link) . '" rel="category tag">' . esc_html($parent_name) . '</a>';
} else {
    // 如果没有父分类,可以选择不显示任何内容,或者显示一个默认消息
    echo 'No parent category';
}
?>

这段代码首先获取当前分类的ID,然后查找其父分类的ID。如果存在父分类,它会获取父分类的名称和链接,并输出一个链接到父分类的超链接。如果当前分类没有父分类(即它是顶级分类),则可以输出一个默认消息或者不显示任何内容。

请确保将这段代码放在你的WordPress主题的适当模板文件中,例如single.php、category.php或者archive.php,这样它就会在相应的页面上显示父分类的名称和链接。

原文

http://wordpress.jianyes.com/jianzhan/470.html

相关推荐
VIP_CQCRE19 小时前
用 Ace Data Cloud 自动发布 WordPress 文章:把 AI 写作变成可积累的 SEO 内容资产
ai·api·seo·wordpress·acedatacloud
VIP_CQCRE7 天前
用 Ace Data Cloud 自动发布 WordPress 文章:把 SEO 内容站变成长期增长资产
ai·自动化·seo·wordpress·acedatacloud
2601_9657984711 天前
Renovate Theme Setup: Fast Contractor & Construction Site Architecture
theme·wordpress
2601_9657984713 天前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
2601_9657984715 天前
Boutique Business Consulting WordPress Architecture & SEO Setup
android·theme·wordpress
2601_9657984716 天前
Plastic Surgery Clinic Web Setup: Deploying Rejuvita WordPress
php·theme·wordpress
2601_9657984716 天前
Contractor Web Setup: Deploying Bathrooms & Kitchens Theme Fast
前端·web3·php·wordpress
e6zzseo23 天前
wordpress独立站seo怎么做才有效
seo·wordpress·独立站·内容优化·外链建设
Web极客码25 天前
如何让用户订阅WordPress评论?提升用户互动的有效策略
运维·服务器·wordpress
fobwebs25 天前
Wordpress Xstore 主题安装Elementor Pro 后找不到某些Elements 组件的解决方法
element·wordpress·form·xstore·pro elements·elementor pro