wordpress woocommerce 如何获取当前类别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24138701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
woocommerce how to get current category
提问by Panagiotis
I am accessing archive-products.php
on woocommerce to display my products (like the normal process in woocommerce).
我正在访问archive-products.php
woocommerce 以显示我的产品(就像 woocommerce 中的正常流程)。
On the page of archive-products.php I have added the sidebar with all the product categories that my shop has (with or without products). I have used the following code to do so:
在 archive-products.php 的页面上,我添加了侧边栏,其中包含我的商店拥有的所有产品类别(有或没有产品)。我已经使用以下代码来做到这一点:
$taxonomy = 'product_cat';
$orderby = 'ID';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '<h2>' . _x('Our Products', 'mfarma') . '</h2>';
$hide_empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'order' => 'ASC',
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $hide_empty
);
?>
<ul>
<?php wp_list_categories($args); ?>
</ul>
Now the left side of the page has the above sidebar and the right one has the products. In each product category I have added a small description with an html format that I want to show when the user has clicked the category. According to woocommerce when you go to a specific category (in my case, http://localhost/product-category/mycategory
) it is still the archive-products.php.
现在页面左侧有上面的侧边栏,右侧有产品。在每个产品类别中,我都添加了一个 html 格式的小说明,当用户单击该类别时,我想显示该说明。根据woocommerce,当您转到特定类别(在我的情况下http://localhost/product-category/mycategory
)时,它仍然是archive-products.php。
I am trying to get the term_id from the link clicked, but the loop (and the global $post) points me to the first product of the list instead of the category that I need. So if a category has zero products, I can't get the term ID
. How do I get that term ID from archive-products.php?
我试图从单击的链接中获取 term_id,但循环(和全局 $post)将我指向列表的第一个产品,而不是我需要的类别。因此,如果一个类别的产品为零,我将无法获得term ID
. 如何从archive-products.php 获取该术语ID?
回答by Panagiotis
Found the answer for something else but it also applies to my question.
找到了其他问题的答案,但它也适用于我的问题。
add_action('woocommerce_archive_description', 'woocommerce_category_description', 2);
function woocommerce_category_description() {
if (is_product_category()) {
global $wp_query;
$cat = $wp_query->get_queried_object();
echo "CAT IS:".print_r($cat,true); // the category needed.
}
}
回答by ?tefan Ondá?
You can do it simplier:
Print current category:
您可以更简单地做到这一点:
打印当前类别:
single_cat_title(); // this prints your current category
Get current category string:
获取当前类别字符串:
single_cat_title('', false); // this returns your current category
echo single_cat_title('', false); // for print current category