在其他阵列上随机显示一个数组

时间:2015-04-01 04:08:48

标签: php arrays wordpress

我需要在其他数组上随机显示一个数组。

例如:我需要在$advertises上显示$abc,但它会显示很多帖子。

$args =array(
        'post_type' => 'post',
        'meta_query' => array(
            'relation'=>'OR',
                        array(
                             'key'  => 'to_event_date',
                             'value' => date('Y/m/d'),
                             'type'  => 'date',
                             'compare'=>'>=',
                         ),
            'relation'=>'OR',
                        array(
                             'key'  => 'to_event_date',
                             'value' => '',
                             'compare'=>'=',
                        )
         )
);

$test = query_posts( $args );

 $advertise1 = array(
                    'posts_per_page' => 1,
                    'order' => 'DESC',
                    'post_type' => 'advertise',
                    'post_status' => 'publish',
                    'suppress_filters' => true
               );

 $advertises=get_posts($advertise1);
 $rand_position=rand(0,2);

 foreach($advertises as $mpost){
    $advertise=$mpost;
 }
 array_splice($test, $rand_position, 0, $advertise);

有人帮助我吗?

2 个答案:

答案 0 :(得分:1)

这段代码很好:

$args =array(
        'post_type' => 'post',
        'meta_query' => array(
            'relation'=>'OR',
                        array(
                             'key'  => 'to_event_date',
                             'value' => date('Y/m/d'),
                             'type'  => 'date',
                             'compare'=>'>=',
                         ),
            'relation'=>'OR',
                        array(
                             'key'  => 'to_event_date',
                             'value' => '',
                             'compare'=>'=',
                        )
         )
);

$test = query_posts( $args );

 $advertise1 = array(
                    'posts_per_page' => 1,
                    'order' => 'DESC',
                    'post_type' => 'advertise',
                    'post_status' => 'publish',
                    'suppress_filters' => true
               );

 $advertises=get_posts($advertise1);
 $rand_position=rand(0,2);


 array_splice($test, $rand_position, 0, $advertises);

答案 1 :(得分:0)

您好我已经解决了这个问题

替换

array_splice($ test,$ rand_position,0,$ advertise); 至 array_splice($ test,$ rand_position,0,$ advertises);