Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Wordpress中获取一个或多个子类别-是否可以合并数组?_Php_Wordpress_Function_Array Merge - Fatal编程技术网

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

Php 在Wordpress中获取一个或多个子类别-是否可以合并数组?,php,wordpress,function,array-merge,Php,Wordpress,Function,Array Merge,我编写了一些代码,使用Wordpress get_terms函数显示用户定义类别的子类别列表,并显示每个子类别的相关缩略图。据我所知,没有一个WP函数可以显示多个家长的所有子类别,所以我想知道是否有可能合并两个或多个get_terms结果的结果 到目前为止,我编写的代码可以很好地从一个父类中获取_术语,但我不确定从这里可以走到哪里 function get_wc_child_cat_thumbs($catParent, $listClassName) { // Our wordpres

我编写了一些代码,使用Wordpress get_terms函数显示用户定义类别的子类别列表,并显示每个子类别的相关缩略图。据我所知,没有一个WP函数可以显示多个家长的所有子类别,所以我想知道是否有可能合并两个或多个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`
函数get\u wc\u child\u cat\u thumbs($catParent,$listClassName){ //我们的wordpress获取\u术语参数 $args=数组( “number”=>$number, 'orderby'=>$orderby, “订单”=>$order, 'hide_empty'=>$hide_empty, '包括'=>$id, “父项”=>$catParent, ); //得到条件 $product\U CATEGORES=获取术语('product\U cat',$args); //查看是否返回任何结果 $count=计数($product\u categories); //如果有,则用子类别详细信息填充列表 如果($count>0){ echo“
    ”; foreach($product\U CATEGORES作为$product\U category){ //获取子类别的缩略图id $thumbnail\u id=获取商业术语元($product\u category->term\u id,'thumbnail\u id',true); //显示结果… 回显“
  • ”.wp_获取附件图像($thumbnail_id)。“
    ”。$product_category->term_id.-
  • ”; //echo$image=wp\u get\u attachment\u image($thumbnail\u id); } 回声“
”; }//如果($count>0)结束{ }//结束函数get\u wc\u child\u cat\u thumbs` 是否可以更改
$catParent
参数,使其接受单个值或数组值,并在某个位置添加数组合并?

是否可行

$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 );
}
foreach($product\U CATEGORES作为$product\U category){
//获取子类别的缩略图id
$thumbnail\u id=获取商业术语元($product\u category->term\u id,'thumbnail\u id',true);
//显示结果…
回显“
  • ”.wp_获取附件图像($thumbnail_id)。“
    ”。$product_category->term_id.-
  • ”; //echo$image=wp\u get\u attachment\u image($thumbnail\u 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
    
    //用于替代品牌插件-正在复制内容
    函数get\u multiple\u woocommerce\u child\u cats($catParent,$listClassName){
    $product_categories=array();
    foreach($catParent作为$parent){
    $args=数组(
    “number”=>$number,
    'orderby'=>$orderby,
    “订单”=>$order,
    'hide_empty'=>$hide_empty,
    '包括'=>$id,
    “父项”=>$parent,
    );
    //得到条件
    $product_categories[]=获取_术语('product_cat',$args);
    }
    //查看是否返回了任何结果
    $count=计数($product\u categories);
    //如果有,则用子类别详细信息填充列表
    如果($count>0){
    //将单独的产品类别数组合并到一个大数组中
    $mergedcats=调用用户函数数组($array\U merge',$product\U categories);
    //函数按名称值对数组排序。需要使用Wordpress$a->name中的类运算符,而不是$a['name'],否则会出现错误
    功能分类($a,$b){
    返回strcmp($a->name,$b->name);
    }
    //按类别名称值对大数组排序
    usort($mergedcats,分拣);
    //用于测试…
    //回声“;
    //打印(结果);
    //回声“;
    //以所需格式打印结果
    echo“
      ”; foreach($mergedcats作为$product\U类别){ //获取子类别的缩略图id $thumbnail\u id=获取商业术语元($product\u category->term\u id,'thumbnail\u id',true); //显示结果… 回声'
    • Cat id为-“.$product\U category->term\U id”。

      Cat名称为-

    • '; }//结束foreach 回声“
    ”; }else{} }//结束函数get\u wc\u child\u cat\u thumbs
    Hi@RST,如果我更改代码并向我的函数调用添加数组,比如
    get_wc_child_cat_thumbs(数组(415418),“testlist”)
    我得到了以下错误…
    可捕获的致命错误:WP_类错误的对象无法在中转换为字符串
    您是否调整了已有的
    foreach
    循环?它不是一个
    数组
    而是一个
    数组
    数组。使用双
    foreach
    它应该可以工作。否,我没有试过——我不知道怎么写:-(很棒的东西——很管用,谢谢:-)第二个数组添加在最后,我希望合并所有子类别,以便按字母顺序对整个批次进行排序。是否需要进行数组合并?您可以在第二个
    foreach
    发生之前操作
    $product\u categories
    ,或者将
    $product\u categories
    数组展平到一维al数组。总是有更多的方法做事情。调整了代码。