user2982427
user2982427

Reputation:

Simplepie - parsing multiple categories per item

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

Upvotes: 2

Views: 614

Answers (1)

Revent
Revent

Reputation: 2109

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.

Upvotes: 1

Related Questions