duandang2123 2013-12-16 17:35
浏览 12
已采纳

SimplePie:按项目值和项目数过滤

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; ?>
  • 写回答

1条回答 默认 最新

  • dtrgqjcd877528 2013-12-16 21:50
    关注

    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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog