我有这个功能:
<
?php
$categories = [];
//array to store all of your category slugs
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->
slug) ){
array_push($categories, $category_list_item->
slug);
}
}foreach ($categories as $category) {
$args = array( 'post_type' =>
'product', 'posts_per_page' =>
100, 'product_cat' =>
$category, 'orderby' =>
'rand' );
?>
很好, 它为我循环返回了类别的slug。问题是, 现在我无法再获取类别名称。我尝试添加另一个数组:’ $ names = []; ’ 并复制了if语句, 在该语句中我用名称替换了slug。但这并没有。在我尝试过的所有解决方案中, 我只返回了仅包含整个名称列表的” 数组” 。有没有人有办法解决吗?
【wordpress-PHP-从类别获取名称和slug】谢谢!
#1
<
?php
$categories = array();
$names = array();
$category_list_items = get_terms( 'product_cat' );
foreach($category_list_items as $category_list_item){
if(! empty($category_list_item->
slug) ){
$categories[] = $category_list_item->
slug;
$names[] = $category_list_item->
name;
}
}foreach ($categories as $key=>
$category) {
echo "Category Name :".$names[$key];
echo "Category Slug :".$category;
$args = array( 'post_type' =>
'product', 'posts_per_page' =>
100, 'product_cat' =>
$category, 'orderby' =>
'rand' );
?>
推荐阅读
- WordPress php.ini文件将upload_max_filesize设置为大约2MB上传
- WordPress的PHP函数通过slug名称获得多个类别()
- WordPress,将变量从page.php传递到header.php
- 找不到页面上的WordPress分页链接
- WordPress(输出帖子标题小写())
- WordPress Nav Walker-多级导航
- WordPress NAV Walker没有显示
- 没有插件的WordPress移动主题切换器(用户代理和Cookie)
- Jenkins自动化部署Jenkins Master修改工作空间