简体   繁体   中英

Getting one or more sub-categories in Wordpress - is merge of arrays possible?

I have written some code that uses the Wordpress get_terms function to display a list of sub-categories for a user-defined category and also shows the relevant thumbnail for each sub-category. As far as I'm aware, there isn't a WP function to display all child categories across multiple parents, so I was wondering if it's possible to merge the results of two or more get_terms results?

The code I have written so far is working fine to get_terms from just one parent category but I'm not sure where to go from here…

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`

Is it possible to change the $catParent argument so that it accepts either single or array values and add in an array_merge somewhere?

Would this work?

$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;
    }
}

assuming $catParent is an array

edit second 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 );
}

I managed to sort the problem (literally) with great help from @RST. In the end I merged the multiple arrays and sorted the results alphabetically using usort and the name value from the array as the comparison. Here's the finished code…

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM