Reputation:
Basically I want to filter only items from various feeds that have a certain category using an of statement, and show only 6 items. I can do successfully either but not both at the same time. I'm trying to first filter the items depending on category and then limit the number of those items to six, or whatever is set. The code is as follows (which does the opposite, I don't understand how to do the other way around). Hope someone helps. Thanks a lot!
<?php
require_once('simplepie131.inc');
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://site1.com/rss',
'http://site2.com/rss',
'http://site3.com/rss',
));
$feed->enable_cache(true);
$feed->set_cache_location('cache');
$feed->set_cache_duration(1800);
$feed->init();
$feed->handle_content_type();
include('header.php');
?>
<h1>Title</h1>
<?php
foreach ($feed->get_items(0, 6) as $item):
?>
<?php
if( $item->get_category()->get_label() == 'category1'
or $item->get_category()->get_label() == 'category2'
or $item->get_category()->get_label() == 'category4'
):
?>
<div>
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title();?></a></h2>
<img src="<?php echo $item->get_description(); ?>" />
<img src="<?php echo $item->get_feed()->get_image_url(); ?>" />
<?php
echo $item->get_feed()->get_title();
echo $item->get_category()->get_label();
echo $item->get_date('d.m.Y | H:i');
?>
</div> <!-- end div -->
<?php endif;?>
<?php endforeach; ?>
Upvotes: 2
Views: 521
Reputation: 2109
The problem you are running in to is that you are limiting the results to the first 6 articles, and then trying to do your condition inside that loop of 6 items. What you need to do is manually count the number of articles allowed through your condition and then quit when you reach 6.
<?php
$counter = 0;
foreach ($feed->get_items() as $item):
if( $item->get_category()->get_label() == 'category1'
or $item->get_category()->get_label() == 'category2'
or $item->get_category()->get_label() == 'category4'
):
$counter++; // increment your counter
?>
<div>
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title();?></a></h2>
<img src="<?php echo $item->get_description(); ?>" />
<img src="<?php echo $item->get_feed()->get_image_url(); ?>" />
<?php
echo $item->get_feed()->get_title();
echo $item->get_category()->get_label();
echo $item->get_date('d.m.Y | H:i');
?>
</div> <!-- end div -->
<?php endif;?>
<?php if ($counter >= 6) break; // break out of the foreach loop ?>
<?php endforeach; ?>
Upvotes: 1