Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
Arrays 如何将wordpress结果与随机数组组合?_Arrays_Wordpress - Fatal编程技术网

Arrays 如何将wordpress结果与随机数组组合?

Arrays 如何将wordpress结果与随机数组组合?,arrays,wordpress,Arrays,Wordpress,我正试图用下面的代码将随机顺序添加到我的wordpress结果中。我被告知一个调用随机数的数组可能会工作。或者尝试在某个地方包含orderby=“rand”参数 类WC_Widget_Brand_缩略图_MJ扩展WP_Widget{ /** Variables to setup the widget. */ var $woo_widget_cssclass; var $woo_widget_description; var $woo_widget_idbase; var $woo_widget_

我正试图用下面的代码将随机顺序添加到我的wordpress结果中。我被告知一个调用随机数的数组可能会工作。或者尝试在某个地方包含orderby=“rand”参数

类WC_Widget_Brand_缩略图_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
}
/**用于设置小部件的变量*/
var$woo_widget_cssclass;
var$woo_小部件描述;
var$woo_widget_idbase;
var$woo_widget_name;
/**建造师*/
函数_u构造(){
/*小部件变量设置*/
$this->woo_widget_name=uuuuuu('WooCommerce品牌缩略图MJ','wc_品牌');
$this->woo_widget_description=uuu('显示随机品牌(供应商)缩略图','wc_品牌');
$this->woo_widget_idbase='wc_brands_brand_缩略图';
$this->woo_widget_cssclass='widget_brand_缩略图';
/*小部件设置*/
$widget\u ops=array('classname'=>$this->woo\u widget\u cssclass,'description'=>$this->woo\u widget\u description);
/*创建小部件*/
$this->WP\u Widget($this->woo\u Widget\u idbase,$this->woo\u Widget\u name,$Widget\u ops);
}
/**@见WP_小部件*/
函数小部件($args$instance){
摘录($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));
如果(!$brands)
返回;
echo$before_小部件;
如果(!empty($instance['title']))
echo$before_title.$instance['title'].$before_title;
woocommerce_get_模板('widgets/brand thumbnailsMJ.php',数组(
“品牌”=>$brands,
'columns'=>$instance['columns']
),“woocommerce brands”,未跟踪的Lashit(插件目录路径(目录名(目录名(uuu文件)))。/templates/);
echo$after_小部件;
}
/**@请参阅WP\u小部件->更新*/
函数更新($new\u实例,$old\u实例){
$instance['title']=条带标记(条带斜杠($new_instance['title']);
$instance['columns']=strip_标记(stripslashes($new_instance['columns']);
$instance['orderby']=条带标记(条带斜杠($new_instance['orderby']);
$instance['exclude']=strip_标记(stripslashes($new_instance['exclude']);
$instance['hide_empty']=条带标记(条带斜杠($new_instance['hide_empty']);
$instance['number']=条带标记(条带斜杠($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']='';
返回$instance;
}
/**@请参阅WP_Widget->form*/
函数形式($instance){
如果(!isset($instance['hide_empty']))
$instance['hide_empty']=0;
如果(!isset($instance['orderby']))
$instance['orderby']='name';
?>


如果您只想随机化从
get\u terms
调用返回的
$brands
的顺序,您可以通过调用
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 );
}

谢谢,我尝试将代码粘贴到所有函数中,但结果不是随机的。我如何编写和调用shuffle($brands)函数。很抱歉,我对php有点陌生。我添加了这个表单元素来调用代码。在
小部件
函数中,您应该添加调用
shuffle($brands)
在以
$brands=get_terms(
开头的行之后。或者,如果您想用orderby参数控制洗牌,请用我在回答中提供的代码块完全替换该行。嗯,这两个选项都不起作用。是否与提取($args)有关;至于为什么$args不起作用?如果只是添加
shuffle($brands);
call不起作用,那么我建议的另一个解决方案也不会有更好的效果。我恐怕没有其他建议。