wordpress-PHP-从类别获取名称和slug

我有这个功能:

<?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。但这并没有。在我尝试过的所有解决方案中, 我只返回了仅包含整个名称列表的”数组”。有没有人有办法解决吗?

谢谢!


#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' );
?>

来源:

https://www.srcmini02.com/67708.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_36830.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?