Mixed posts & tweets in one loop
/* Array short by date */
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
function sortByOrder($a, $b) {
return $a['timestamp'] - $b['timestamp'];
}
/** Display Post and Tweets in same Loop */
function post_with_tweets($number_of_posts){
// Array for all
$all = array();
// Twitter Authorization
@require_once 'twitteroauth/twitteroauth.php';
$twitterConnection = new TwitterOAuth(
'wa0IsTACrMG6LO8Y7yVsA', // Consumer Key
'e9OSB16HWCL4GAywWnRRoeNnZXrcNkDSGdbIA2igc4', // Consumer secret
'177701324-2ocF2JVrEKbvcXEdqlgoD4mSnFuUDvABSzien29B', // Access token
'7Y5g9nS3uZnxPb6aDmeZgumujW0zKlwt2UDmllkzAEMxp' // Access token secret
);
$twitterData = $twitterConnection->get(
'statuses/user_timeline',
array(
'screen_name' => 'mercuryeng', // Your Twitter Username
'count' => 99, // Number of Tweets to display
'exclude_replies' => true
)
);
if($twitterData && is_array($twitterData)) {
// Tweets List
foreach($twitterData as $tweet):
$latestTweet = $tweet->text;
$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
$twitterTime = strtotime($tweet->created_at);
$new_time = $tweet->created_at;
$tweettime = new DateTime($new_time);
$tweettime = $tweettime->format('H:i - d/m/Y');
$tweeturl = $tweet->entities;
$tweeturl = $tweeturl->urls;
$tweeturl = $tweeturl[0];
$tweeturl = $tweeturl->url;
$tweetbg = $tweet->user;
$tweetbgcolour = $tweetbg->profile_background_color;
$tweetbg = $tweetbg->profile_background_image_url;
$temp = array('id'=>$tweet->id, 'title'=>$latestTweet, 'time'=>$tweettime, 'timestamp'=>$twitterTime, 'url'=>$tweeturl, 'image'=>$tweetbg, 'colour'=>$tweetbgcolour, 'type'=>2);
$all[]=$temp;
endforeach;
// Posts List
$args = array('post_type' => 'post', 'posts_per_page' => '999', 'order' => 'DESC', 'orderby' => 'date' );
$newsPost = get_posts( $args );
foreach($newsPost as $post):
$posttime = new DateTime($post->post_date);
$posttime = $posttime->format('d/m/Y');
$temp = array('id'=>$post->ID,'title'=>$post->post_title, 'time'=>$posttime, 'timestamp'=>strtotime($post->post_date), 'url'=>$post->guid, 'image'=>'', 'colour'=>'', 'type'=>1);
$all[]=$temp;
endforeach;
// Short by Date
usort($all, 'sortByOrder');
// Change order
$all=array_reverse($all);
// Get all tweets and posts
$i = 1;
foreach($all as $post) :
if($post['type'] == 1) { ?>
<!-- POST -->
<?php $post_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post['id'] ), 'home-post-thumb' ); ?>
<?php $post_link = get_permalink( $post['id'] ); ?>
<a class="news_single news_post" href="<?php echo $post_link; ?>" style="background-image: url('<?php echo $post_thumb[0]; ?>');">
<h2><?php echo $post['title']; ?></h2>
<p class="the_time"><?php echo $post['time']; ?></p>
</a>
<?php } else { ?>
<!-- TWEET -->
<div class="news_single news_tweet">
<h2><?php echo $post['title']; ?></h2>
<p class="the_time"><?php echo $post['time']; ?></p>
</div>
<?php }
if ($i == $number_of_posts) break;
$i++;
endforeach;
}
}