如何将我的wordpress结果与随机数组合并?

时间:2013-06-21 16:08:17

标签: arrays wordpress wordpress-plugin

我正在尝试使用下面的代码为我的wordpress结果添加随机顺序。我被告知一个调用随机数的数组可能会起作用。或者以某种方式尝试在某处包含orderby =“rand”参数。

类WC_Widget_Brand_Thumbnails_MJ扩展了WP_Widget {

/** Variables to setup the widget. */
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;

/** constructor */
function __construct() {

    /* Widget variable settings. */
    $this->woo_widget_name          = __('WooCommerce Brand Thumbnails MJ', 'wc_brands' );
    $this->woo_widget_description   = __( 'Show a RANDOM Brand (Supplier) Thumbnail', 'wc_brands' );
    $this->woo_widget_idbase        = 'wc_brands_brand_thumbnails';
    $this->woo_widget_cssclass      = 'widget_brand_thumbnails';

    /* Widget settings. */
    $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );

    /* Create the widget. */
    $this->WP_Widget( $this->woo_widget_idbase, $this->woo_widget_name, $widget_ops );
}

/** @see WP_Widget */
function widget( $args, $instance ) {
    extract( $args );

    $exclude = array_map( 'intval', explode( ',', $instance['exclude'] ) );
    $order = $instance['orderby'] == 'name' ? 'asc' : 'desc';

    $brands = get_terms( 'product_brand', array( 'hide_empty' => $instance['hide_empty'], 'orderby' => $instance['orderby'], 'exclude' => $exclude, 'number' => $instance['number'], 'order' => $order ) );

    if ( ! $brands ) 
        return;

    echo $before_widget;

    if ( ! empty( $instance['title'] ) )
        echo $before_title . $instance['title'] . $after_title;

    woocommerce_get_template( 'widgets/brand-thumbnailsMJ.php', array(
        'brands'    => $brands,
        'columns'   => $instance['columns']
    ), 'woocommerce-brands', untrailingslashit( plugin_dir_path( dirname( dirname( __FILE__ ) ) ) ) . '/templates/' );

    echo $after_widget;
}

/** @see WP_Widget->update */
function update( $new_instance, $old_instance ) {
    $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
    $instance['columns'] = strip_tags( stripslashes( $new_instance['columns'] ) );
    $instance['orderby'] = strip_tags( stripslashes( $new_instance['orderby'] ) );
    $instance['exclude'] = strip_tags( stripslashes( $new_instance['exclude'] ) );
    $instance['hide_empty'] = strip_tags( stripslashes( $new_instance['hide_empty'] ) );
    $instance['number'] = strip_tags( stripslashes( $new_instance['number'] ) );

    if ( ! $instance['columns'] )
        $instance['columns'] = 1;

    if ( ! $instance['orderby'] )
        $instance['orderby'] = 'name';

    if ( ! $instance['exclude'] )
        $instance['exclude'] = '';

    if ( ! $instance['hide_empty'] )
        $instance['hide_empty'] = 0;

    if ( ! $instance['number'] )
        $instance['number'] = '';

    return $instance;
}

/** @see WP_Widget->form */
function form( $instance ) {
    if ( ! isset( $instance['hide_empty'] ) ) 
        $instance['hide_empty'] = 0;
    if ( ! isset( $instance['orderby'] ) ) 
        $instance['orderby'] = 'name';
    ?>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php if ( isset ( $instance['title'] ) ) echo esc_attr( $instance['title'] ); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'columns' ); ?>"><?php _e('Columns:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'columns' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'columns' ) ); ?>" value="<?php if ( isset ( $instance['columns'] ) ) echo esc_attr( $instance['columns'] ); else echo '1'; ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e('Number:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" value="<?php if ( isset ( $instance['number'] ) ) echo esc_attr( $instance['number'] ); ?>" placeholder="<?php _e('All', 'wc_brands'); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e('Exclude:', 'wc_brands') ?></label>
            <input type="text" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" value="<?php if ( isset ( $instance['exclude'] ) ) echo esc_attr( $instance['exclude'] ); ?>" placeholder="<?php _e('None', 'wc_brands'); ?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>"><?php _e('Hide empty brands:', 'wc_brands') ?></label>
            <select id="<?php echo esc_attr( $this->get_field_id( 'hide_empty' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'hide_empty' ) ); ?>">
                <option value="1" <?php selected( $instance['hide_empty'], 1 ) ?>><?php _e('Yes', 'wc_brands') ?></option>
                <option value="0" <?php selected( $instance['hide_empty'], 0 ) ?>><?php _e('No', 'wc_brands') ?></option>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e('Order by:', 'wc_brands') ?></label>
            <select id="<?php echo esc_attr( $this->get_field_id( 'orderby' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'orderby' ) ); ?>">
                <option value="name" <?php selected( $instance['orderby'], 'name' ) ?>><?php _e('Name', 'wc_brands') ?></option>
                <option value="count" <?php selected( $instance['orderby'], 'count' ) ?>><?php _e('Count', 'wc_brands') ?></option>

            </select>
        </p>
    <?php
}

}

1 个答案:

答案 0 :(得分:1)

如果您只想随机调整$brands来自get_terms来自shuffle($brands);来电的订单,您可以通过调用orderby

来简单地改变结果

如果您希望能够根据$args = array( 'hide_empty' => $instance['hide_empty'], 'orderby' => $instance['orderby'], 'exclude' => $exclude, 'number' => $instance['number'], 'order' => $order ); if ($instance['orderby'] == 'random') { $args['orderby'] = 'none'; $brands = get_terms( 'product_brand', $args ); shuffle($brands); } else { $brands = get_terms( 'product_brand', $args ); } 参数选择是否对结果进行混洗,那么您需要更多代码。

{{1}}