我希望每次在WooCommerce中始终显示4个类别框

时间:2019-11-12 作者:Ohi Chowdhury

我的网站有问题。我正在使用一个短代码来显示一个部分,该部分每次显示4个rand类别。我的意思是每次重新加载时它都会发生变化。现在的问题是,有时它会显示所有4个兰德。有时是3或2。这里是我的代码:

function rand_product_categories( $atts ) {
    global $woocommerce_loop;

    $atts = shortcode_atts( array(
        \'number\'     => null,
        \'orderby\'    => \'name\',
        \'order\'      => \'ASC\',
        \'columns\'    => \'4\',
        \'hide_empty\' => 1,
        \'parent\'     => \'\',
        \'ids\'        => \'\'
    ), $atts );

    if ( isset( $atts[\'ids\'] ) ) {
        $ids = explode( \',\', $atts[\'ids\'] );
        $ids = array_map( \'trim\', $ids );
    } else {
        $ids = array();
    }

    $hide_empty = ( $atts[\'hide_empty\'] == true || $atts[\'hide_empty\'] == 1 ) ? 1 : 0;

    // get terms and workaround WP bug with parents/pad counts
    $args = array(
        \'orderby\'    => $atts[\'orderby\'],
        \'order\'      => $atts[\'order\'],
        \'hide_empty\' => $hide_empty,
        \'include\'    => $ids,
        \'pad_counts\' => true,
        \'child_of\'   => $atts[\'parent\']
    );

    $rand_product_categories = get_terms( \'product_cat\', $args );

    shuffle( $rand_product_categories );
    $rand_product_categories = array_slice( $rand_product_categories, 0, 10 );


    if ( \'\' !== $atts[\'parent\'] ) {
        $rand_product_categories = wp_list_filter( $rand_product_categories, array( \'parent\' => $atts[\'parent\'] ) );
    }

    if ( $hide_empty ) {
        foreach ( $rand_product_categories as $key => $category ) {
            if ( $category->count == 0 ) {
                unset( $rand_product_categories[ $key ] );
            }
        }
    }

    if ( $atts[\'number\'] ) {
        $rand_product_categories = array_slice( $rand_product_categories, 0, $atts[\'number\'] );
    }

    $columns = absint( $atts[\'columns\'] );
    $woocommerce_loop[\'columns\'] = $columns;

    ob_start();

    // Reset loop/columns globals when starting a new loop
    $woocommerce_loop[\'loop\'] = $woocommerce_loop[\'column\'] = \'\';

    if ( shuffle($rand_product_categories) ) {
        woocommerce_product_loop_start();

        foreach ( $rand_product_categories as $category ) {
            wc_get_template( \'content-product_cat.php\', array(
                \'category\' => $category
            ) );
        }

        woocommerce_product_loop_end();
    }

    woocommerce_reset_loop();

    return \'<div class="woocommerce columns-\' . $columns . \'">\' . ob_get_clean() . \'</div>\';
}
add_shortcode( \'rand_prod_cat\', \'rand_product_categories\' );
循环正常。提前感谢

1 个回复
最合适的回答,由SO网友:Kalimah 整理而成

您的代码运行良好。我在当地的WordPress上进行了测试,并按预期工作。

columns 属性是视觉表示,因此它不会影响显示的类别数。number 属性是检索到的类别数。我把它改为4,效果很好。

无论哪种方式,您都可以像这样优化代码:

function rand_product_categories( $atts ) {
    global $woocommerce_loop;

    // Get shortcode attributes and set default values
    $atts = shortcode_atts( array(
        \'number\'     => 10,
        \'orderby\'    => \'name\',
        \'order\'      => \'ASC\',
        \'columns\'    => \'4\',
        \'hide_empty\' => 1,
        \'parent\'     => 0,
        \'ids\'        => \'\'
    ), $atts );

    if ( isset( $atts[\'ids\'] ) ) {
        $ids = explode( \',\', $atts[\'ids\'] );
        $ids = array_map( \'trim\', $ids );
    } else {
        $ids = array();
    }

    $hide_empty = ( $atts[\'hide_empty\'] == true || $atts[\'hide_empty\'] == 1 ) ? 1 : 0;

    // get terms and workaround WP bug with parents/pad counts
    $args = array(
        \'orderby\'    => $atts[\'orderby\'],
        \'order\'      => $atts[\'order\'],
        \'hide_empty\' => $hide_empty,
        \'include\'    => $ids,
        \'pad_counts\' => true,
        \'child_of\'   => $atts[\'parent\']
    );

    $rand_product_categories = get_terms( \'product_cat\', $args );

    shuffle( $rand_product_categories );

    // add $atts[\'number\'] here
    $rand_product_categories = array_slice( $rand_product_categories, 0, $atts[\'number\'] );

    // child_of default value is 0, no need to check for parent
    /*if ( \'\' !== $atts[\'parent\'] ) {
        $rand_product_categories = wp_list_filter( $rand_product_categories, array( \'parent\' => $atts[\'parent\'] ) );
    }*/


/*
    // No need to check for hide empty, its already part of the query
    if ( $hide_empty ) {
        foreach ( $rand_product_categories as $key => $category ) {
            if ( $category->count == 0 ) {
                unset( $rand_product_categories[ $key ] );
            }
        }
    }
*/
 /*   if ( $atts[\'number\'] ) {
        $rand_product_categories = array_slice( $rand_product_categories, 0, $atts[\'number\'] );
    }*/

    $columns = absint( $atts[\'columns\'] );
    $woocommerce_loop[\'columns\'] = $columns;

    ob_start();

    // Reset loop/columns globals when starting a new loop
    $woocommerce_loop[\'loop\'] = $woocommerce_loop[\'column\'] = \'\';

    // Shuffle already done 
   // if ( shuffle($rand_product_categories) ) {
        woocommerce_product_loop_start();

        foreach ( $rand_product_categories as $category ) {
            wc_get_template( \'content-product_cat.php\', array(
                \'category\' => $category
            ) );
        }

        woocommerce_product_loop_end();
  //  }

    woocommerce_reset_loop();

    return \'<div class="woocommerce columns-\' . $columns . \'">\' . ob_get_clean() . \'</div>\';
}
add_shortcode( \'rand_prod_cat\', \'rand_product_categories\' );

相关推荐

Shortcode to pull posts

我需要一些帮助。我创建了一个短代码来引入帖子,然后使用ajax加载更多帖子。我的一切都正常,但它复制了第一组帖子。下面是我的代码。这是shortcode函数:add_shortcode( \'articles-grid\', \'articles_grid\' ); function articles_grid( $atts ) { $showdate = $showauthor = $post_meta = $post_author = $post_seperator