2016-06-27 24 views
0

我在WordPress中建立了一個頁面,其中包含Twitter API的推文,然後是標準的get_posts循環。將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

而是直接呼應的結果,你可以把它們放到一個數組,然後用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

+0

爲什麼我不認爲把輸出放在數組中!你先生太棒了。 – Kyon147