WooCommerce获取当前类别页面的存档/类别插件

时间:2016-02-19 作者:Sundaze

我想获取当前存档页的slug。我有一个滑块,我想在其中回应这一类别中的产品。几个小时后,这就是我的全部,但它不起作用。。。

<?php 

if ( is_single() ) {
    $cats =  get_the_category();
    $cat = $cats[0]; // let\'s just assume the post has one category
}
else { // category archives
    $cat = get_category( get_query_var( \'cat\' ) );
}
$cat_name = $cat->name;
$cat_slug = $cat->slug;

echo \'<h2 style="text-align:center;">\'.$cat_name.\' - Spotlight</h2>\';
echo \'<h2 style="text-align:center;">\'.$cat_slug.\' -- Slug</h2>\';

echo "here: ".$cat_name." - ".$cat_slug;

echo do_shortcode(\'[wpb-product-slider title="" product_type="category" category="\'.$cat_slug.\'" items="3" items_desktop="3" items_desktop_small="3" items_tablet="2" items_mobile="1" width="320" height="300" crop="true" theme="ben-box" posts="50" orderby="rand" speed="2000" order="ASC"]\'); ?>
但美元猫总是空的。我该怎么做?

1 个回复
SO网友:LWS-Mo

如果您正在使用“archive.php”模板,可以尝试以下代码:

global $post; //maybe not needed

$category = get_the_category($post->ID);
$slug = $category[0]->slug; // try print_r($category); to see everything

echo $slug;

相关推荐

Get term by slug in Gutenberg

在古腾堡,可以使用getEntityRecord 通过术语id获取特定分类法术语数据的选择器:// Get the category term with term id = 25 const term = useSelect( ( select ) => select(\'core\').getEntityRecord( \'taxonomy\', \'category\', 25 ) ); 我的问题是,有没有办法通过使用术语slug而不是术语id来获取特定