php - 获取所有类别和帖子模板上的 WordPress 当前类别 ID 和顶级父类别 ID

标签 php wordpress

假设您的 WordPress 网站具有以下结构:

Dog (Top Level Category)
-Boxer (Sub Level Category)
--Brindle (Third Level Category)
--Reverse Brindle (Third Level Category)
-Golden Retriever (Sub Level Category)
-Labrador (Sub Level Category)

Cat (Top Level Category)
-Siamese (Sub Level Category)
--Siamese Black (Third Level Category)
--Siamese White (Third Level Category)
-Bengal (Sub Level Category)
-Ragdoll (Sub Level Category)

尝试开发一个功能来添加到所有页面模板(例如category.php、single.php等),以便侧面菜单将根据您的位置显示完整的类别层次结构。例如,如果您正在查看狗类别、拳师犬或斑纹犬,它将显示与上面相同的完整狗菜单,每个菜单都链接到各自的类别,并且您所在的类别将有一个事件类别。

还在页面顶部输出顶级类别和您所在的类别。例如,如果在 Reverse Brindle 上会说“Dog > Reverse Brindle”,或者如果在 Dog 上只会说“Dog”,或者如果在 Boxer 上会说“Dog > Boxer”。

它可以正常工作,但有很多冗余代码,并且在同一页面上无法按预期工作。

最佳答案

这里有一种方法,您可以使用 wp_list_categories() 显示所有类别的完整结构化列表。 .

<?php
//Get whatever object we're working with (category or post?)
$thisObj = get_queried_object();

//If it's a post, get the category ID
if(!is_null($thisObj->ID)){
    $currentCat = get_the_category();
    $currentCatID = $currentCat[0]->cat_ID;
}

//If it's a category, get the ID a different way
elseif(!is_null($thisObj->term_id)){
    $currentCatID = $thisObj->term_id;
}

//Call wp_list_categories to echo the list of categories
$args = array(
    'current_category' => $currentCatID,  //This assigns the "current-cat" class to the correct item in the list so you can style it differently
    'child_of' => 0,
    'hide_empty' => 0,
    'order' => 'ASC',
    'orderby' => 'name',
);
wp_list_categories($args);

然后您可以使用以下内容设置列表中事件类别的样式:

(您还可以使用更多的类来设置列表其余部分的样式。只需查看它生成的 HTML)

.current-cat{
    /* whatever you want */
}

这应该在页面顶部完成:

<?php
$thisObj = get_queried_object(); // Find out what we're displaying (Category or post?)

if(!is_null($thisObj->ID)){  // If it's a post, get the category ID
    $currentCat = get_the_category();
    $currentCatID = $currentCat[0]->cat_ID;
}
elseif(!is_null($thisObj->term_id)){  // If it's a category, get the ID a different way
    $currentCatID = $thisObj->term_id;
}

// Get the name of the Category we're starting with
$currentCatName = get_cat_name($currentCatID);

//Get the ID then the name of the highest parent Category
$topParentID = end(get_ancestors($currentCatID, 'category'));
$topParentName = get_cat_name($topParentID);


if(!$topParentName){  // If we're already at the highest category, just save the name
    $finalAnswer = $currentCatName;
}
else{  // Otherwise, display "Top Parent > Current Cat"
    $finalAnswer = $topParentName . ' > ' . $currentCatName;
}
echo $finalAnswer; //Ta-da!

关于php - 获取所有类别和帖子模板上的 WordPress 当前类别 ID 和顶级父类别 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49265865/

相关文章:

php - 如何通过电子邮件发送数据库中的一长串电子邮件?

php - 为什么组合键 Ctrl-K Ctrl-F 不适用于 php 可视代码?

php - 从链接 Wordpress 中删除站点 url

php - 将 WooCommerce 自定义 session 变量保存为订单元数据

css - Wordpress CSS,为某种类型的所有小部件选择元素?

php - 在事务中使用 MySQL 变量和 PHP 变量

php - sql查询的问题

php - WordPress "s"查询 $args 中有几个关键字

php - 在 Woocommerce 中以编程方式创建新的产品属性

php - WooCommerce:CSS - 定位特定产品类别