0

i am having problem in my website.i am using a shortcode to show a section which is show 4 rand category each time.i mean its changes each time reloads. now the problem is sometime its showing all 4 rand.sometime it 3 or 2.here my code:

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' );

and the loops is ok.thanks in advance

  • How many categories do you have in total? – Kalimah Nov 12 at 9:36
  • sorry for late.i have around 30 categories – Ohi Chowdhury Nov 12 at 9:55
  • And the maximum number of categories you see is 4? – Kalimah Nov 12 at 9:55
  • i have limited the columns to 4 you can see.and sometimes it only shows 1 , sometime 2/3, – Ohi Chowdhury Nov 12 at 10:05
  • i need to show rand category everytime 4.but randomized. – Ohi Chowdhury Nov 12 at 10:06
0

Your code is working fine. I tested on local WordPress and worked as expected.

columns attribute is the visual presentation so it will not affect how many categories are displayed. number attribute is the number of categories retrieved. I changed it to 4 and it worked perfectly.

Either way you can optimize your code like this:

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' );
  • thanks.does it shows 4 random categories, everytime refresh? – Ohi Chowdhury Nov 12 at 10:30
  • i just tested.it works fine.thanks :) – Ohi Chowdhury Nov 12 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.