dongpochi9741 2014-11-13 11:28 采纳率: 100%
浏览 76

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

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?

  • 写回答

2条回答 默认 最新

  • dsen53898 2014-11-13 11:39
    关注

    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 );
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?