2

Trying to parse rss feed with items that have multiple categories per item. The original file is an atom structured feed which I have parsed using simplexml and outputed certain elements as an rss feed. The multiple categories in the original atom file are stated as attributes to the category element. I'm trying to display items based on any category defined. As it is now simplepie only recognizes the first category. The simplified code is as follows:

<item>
    <title>Banana</title>
    <category>Fruit</category>
    <category>Yellow</category>
</item>


<item>
    <title>Apple</title>
    <category>Round</category>
    <category>Fruit</category>
</item>

// display all titles from items with category 'Fruit'

<?php



foreach ($feed->get_items() as $item): 
        if( 
            $item->get_category()->get_label() == 'Fruit' 
        ):

    echo $item->get_title();

endforeach; 


// result - displays only Banana but not Apple
  • It sounds like the get_category function only reads the first one it encounters. If you print out `$item->get_category()` to the screen, what do you get? You may need to extend the class... – Revent Jan 17 '14 at 23:12

1 Answers1

1

In the latest version (1.3.1 as of this post), there are two functions in the item class:

public function get_category($key = 0)

and

public function get_categories()

You could either use the first and pass in the key of the category you want, or just use the second function, get all the categories (get_categories documentation) and and use PHP's array_search function.

Revent
  • 2,091
  • 2
  • 18
  • 33