将get_posts循环与另一个数组相结合,并按日期随机排序

时间:2016-06-27 11:23:32

标签: php arrays wordpress twitter

我有一个我在WordPress中构建的页面,其中包含Twitter API的推文,然后是标准的get_posts循环。

目前他们正在逐一展示,但我希望创建一个包含两者的砌体样式网格。

有没有办法按日期在彼此之间输出两个数组,所以项目被分解而不是先输出推文而不是帖子?

这是我的代码。

$twitter = new TwitterAPIExchange($settings);

$string = json_decode($twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest(),$assoc = TRUE);

echo '<div class="funny-grid">';
echo '<div class="grid-sizer"></div>';
foreach($string as $items)
    {
    $string = $items['text'];
    $regex = "@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@";

    echo '<div class="grid-item tweet ">';
        echo '<i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>';
        echo '<div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' ."<br />";
        echo '<div class="tweet-user">' . '@' . '<a href="http://www.twitter.com/' . $items['user']['screen_name'] . '">' . $items['user']['screen_name'] . '</a>' . '</div>';
        echo '<div class="tweet-date">' . $items['created_at'] . '</div>' . "<br />";

    echo '</div>';
    }

    //counts the amount of items in the array.
    /*echo "Number of items:" . count($string);*/
?>
<!--<div style="clear: both;"></div>-->

            <?php 
        query_posts('post_type=funny_wall &posts_per_page=8');
        if(have_posts()):while(have_Posts()):the_post();
         $img = wp_get_attachment_url(get_post_thumbnail_id($post->ID), "full");?>
            <div class="grid-item image">
                <div class="as">
                <img src="<?php echo $img ; ?>">
                    <a href="<?php the_permalink();?>">
                    </a>            
                </div>                                  
            </div>  

            <?php
            endwhile;
            endif;
            wp_reset_query();
            ?>

        </div>
        </div>

1 个答案:

答案 0 :(得分:1)

不是直接回显结果,而是将它们放入数组中,然后使用shuffle()进行随机化,然后一次性输出。

例如为: 鸣叫

$grid_items[] =  '<div class="grid-item tweet ">
    <i class="fa fa-lg fa-twitter inverse" aria-hidden="true"></i>
    <div class="tweet-text">' . preg_replace($regex, ' ', $string) . '</div>' .'<br />
    <div class="tweet-user">' . '@' . '<a href="http://www.twitter.com/' . $items['user']['screen_name'] . '">' . $items['user']['screen_name'] . '</a>' . '</div>
    <div class="tweet-date">' . $items['created_at'] . '</div>' . '<br />
    </div>';

帖子:

$grid_items[] = "<div class='grid-item image'>
            <div class='as'>
            <img src=".$img.">
                <a href=".the_permalink().">
                </a>            
            </div>                                  
        </div>";

然后使用:

shuffle($grid_items);
foreach($grid_items as $grid_item){
    echo $grid_item;
}

当你想输出它们时。

http://php.net/manual/en/function.shuffle.php