Tes*_*ter 48 php wordpress woocommerce
我试图通过我的WordPress主题中的函数从WooCommerce获取产品类别
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0]);
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';
echo '<li><a href="'.get_permalink().'">'.$loop->post->post_title.'</a></li>';
echo '<li><a href="">'.get_categories().'</a></li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
Run Code Online (Sandbox Code Playgroud)
上面的代码返回了一些产品,但产品类别.
当我echo '<li><a href="">'.get_categories().'</a></li>';在上面的代码中包含它时,它返回一个数组.我该如何解决?
我如何更改此以从WooCommerce获取产品类别?
Sum*_*n95 129
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$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 = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
echo '<br /><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a>';
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
}
}
?>
Run Code Online (Sandbox Code Playgroud)
这将按层次列出其下的所有顶级类别和子类别.如果您只想显示顶级类别,请不要使用内部查询.根据自己的喜好设计风格.
小智 17
通过添加子类别的链接来改进Suman.hassan95的答案.替换以下代码:
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo $sub_category->name ;
}
}
Run Code Online (Sandbox Code Playgroud)
有:
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<br/><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
}
}
Run Code Online (Sandbox Code Playgroud)
或者如果您还希望每个子类别都有一个计数器,请替换为:
$sub_cats = get_categories( $args2 );
if($sub_cats) {
foreach($sub_cats as $sub_category) {
echo '<br/><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">'. $sub_category->name .'</a>';
echo apply_filters( 'woocommerce_subcategory_count_html', ' <span class="cat-count">' . $sub_category->count . '</span>', $category );
}
}
Run Code Online (Sandbox Code Playgroud)
/**
* Lists all product categories and sub-categories in a tree structure.
*
* @return array
*/
function list_product_categories() {
$categories = get_terms(
array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'hide_empty' => false,
)
);
$categories = treeify_terms($categories);
return $categories;
}
/**
* Converts a flat array of terms into a hierarchical tree structure.
*
* @param WP_Term[] $terms Terms to sort.
* @param integer $root_id Id of the term which is considered the root of the tree.
*
* @return array Returns an array of term data. Note the term data is an array, rather than
* term object.
*/
function treeify_terms($terms, $root_id = 0) {
$tree = array();
foreach ($terms as $term) {
if ($term->parent === $root_id) {
array_push(
$tree,
array(
'name' => $term->name,
'slug' => $term->slug,
'id' => $term->term_id,
'count' => $term->count,
'children' => treeify_terms($terms, $term->term_id),
)
);
}
}
return $tree;
}
Run Code Online (Sandbox Code Playgroud)
它也比当前的最佳答案高效得多,因为它仅使用一个查询。
您也可以使用 wp_list_categories();
wp_list_categories( array('taxonomy' => 'product_cat', 'title_li' => '') );
Run Code Online (Sandbox Code Playgroud)