Reputation: 625
I need to display the last 2 posts of a network of sites. Currently I'm retrieving all latest posts for all sites but only want to display 2 of them.
I have the following code:
<?php
$items = return_latest_posts();
global $wpdb;
$table_search = $wpdb->prefix . "searchinfo";
$query = "SELECT * FROM `$table_search`";
$wpdb->show_errors();
$results = $wpdb->get_results($query);
foreach ($results as $site){
$item = return_latest_posts_single_site($site->username,$site->password,$site->database,$site->host,$site->db_prefix);
?>
<li>
<h4><?php echo $site->sitename; ?></h4>
<?php foreach($item as $res){ ?>
<p class="date"><?php echo $res->post_date ?></p>
<h4><a href="<?php echo $res->guid; ?>" target='_blank'><?php echo $res->post_title ?></a></h4>
<?php } ?>
<hr />
</li>
<?php } ?>
How am I able to only show the last 2 posts of all sites together?
Thanks in advance!
Upvotes: 1
Views: 126
Reputation: 482
You might consider counting the number of times that the $results foreach runs and restricting output to the first two laps:
<?php
$items = return_latest_posts();
global $wpdb;
$table_search = $wpdb->prefix . "searchinfo";
$query = "SELECT * FROM `$table_search`";
$wpdb->show_errors();
$results = $wpdb->get_results($query);
$count = 0;
foreach ($results as $site){
$count++;
$item = return_latest_posts_single_site($site->username,$site->password,$site->database,$site->host,$site->db_prefix);
if ( $count < 3) {
?>
<li>
<h4><?php echo $site->sitename; ?></h4>
<?php foreach($item as $res){ ?>
<p class="date"><?php echo $res->post_date ?></p>
<h4><a href="<?php echo $res->guid; ?>" target='_blank'><?php echo $res->post_title ?></a></h4>
<?php } ?>
<hr />
</li>
<?php }} ?>
Upvotes: 1