[英]Getting one or more sub-categories in Wordpress - is merge of arrays possible?
我编写了一些代码,这些代码使用Wordpress get_terms函数显示用户定义类别的子类别列表,并显示每个子类别的相关缩略图。 据我所知,没有一个WP函数可以显示多个父项中的所有子类别,因此我想知道是否可以合并两个或多个get_terms结果的结果?
到目前为止,我编写的代码对于仅来自一个父类别的get_terms都可以正常工作,但是我不确定从这里开始该去哪里……
function get_wc_child_cat_thumbs($catParent, $listClassName) {
// Our wordpress get_terms arguments
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $catParent,
);
// Get the terms
$product_categories = get_terms( 'product_cat', $args );
// See if any results are returned
$count = count($product_categories);
// If there are, populate a list with the subcategories details
if ( $count > 0 ){
echo '<ul class="'.$listClassName.'">';
foreach ( $product_categories as $product_category ) {
// Get the thumbnail id for the subcategory
$thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
// Show the results…
echo '<li>'.wp_get_attachment_image( $thumbnail_id ).'<br />'.$product_category->term_id.' - <a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></li>';
//echo $image = wp_get_attachment_image( $thumbnail_id );
}
echo '</ul>';
} // END if ( $count > 0 ){
} // END function get_wc_child_cat_thumbs`
是否可以更改$catParent
参数,使其接受单个值或数组值,并在某个位置添加array_merge?
这行得通吗?
$product_categories = array();
foreach ($catParent as $parent) {
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $parent,
);
// Get the terms
$categories = get_terms('product_cat', $args );
foreach ($categories as $category) {
$product_categories[] = $category;
}
}
假设$catParent
是一个数组
编辑第二个foreach
foreach ( $product_categories as $product_category ) {
// Get the thumbnail id for the subcategory
$thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
// Show the results…
echo '<li>'.wp_get_attachment_image( $thumbnail_id ).'<br />'.$product_category->term_id.' - <a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></li>';
//echo $image = wp_get_attachment_image( $thumbnail_id );
}
在@RST的大力帮助下,我设法(从字面上)对问题进行了排序。 最后,我合并了多个数组,并使用usort和数组中的名称值作为比较按字母顺序对结果进行了排序。 这是完成的代码…
// Used instead of brands plugin - was duplicating content
function get_multiple_woocommerce_child_cats($catParent, $listClassName) {
$product_categories = array();
foreach ($catParent as $parent) {
$args = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'parent' => $parent,
);
// Get the terms
$product_categories[] = get_terms( 'product_cat', $args );
}
// See if any results were returned
$count = count($product_categories);
// If there are, populate a list with the subcategories details
if ( $count > 0 ){
// Merge the separate product category arrays into one big array
$mergedcats = call_user_func_array('array_merge', $product_categories);
// Function to sort the array by name value. Need to use the class operator in Wordpress $a->name instead of $a['name'] otherwise we get an error
function sorteed($a, $b) {
return strcmp($a->name, $b->name);
}
// Sort the big array by the category name value
usort($mergedcats, sorteed);
// For testing…
//echo "<pre>";
//print_r($result);
//echo"</pre>";
// Print out the results in the desired format
echo '<ul class="'.$listClassName.'">';
foreach ( $mergedcats as $product_category ) {
// Get the thumbnail id for the subcategory
$thumbnail_id = get_woocommerce_term_meta( $product_category->term_id, 'thumbnail_id', true );
// Show the results…
echo ' <li>
<div>
<a href="' . get_term_link( $product_category ) . '">'.wp_get_attachment_image( $thumbnail_id ).'</a>
</div>
<p>Cat id is - '.$product_category->term_id.'</p><p> Cat name is - <a href="' . get_term_link( $product_category ) . '">' . $product_category->name . '</a></p>
</li>';
} // END foreach
echo '</ul>';
} else {}
} // END function get_wc_child_cat_thumbs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.