在Wordpress中获取一个或多个子类别 - 可以合并数组


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 ( $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