Is there way to make conditional branch for Rss feed items?

Question

I made custom field as Rss feed items. When I try to make conditional branch of these items, it does not work.

This is code I made.
Problem is that ‘color2’ is always ignored however value of ‘color1’ does not exist. My code should show color2 if color1 does not exist.

<?php
$rss = simplexml_load_file('https://xxxxxxxxxxxx.xxxx/?cat=329&feed=rss2');
?>    

<?php
    foreach($rss->channel->item as $item):
        $color1 = $item->color1;
        $color2 = $item->color2;
        }
?>

<span>
<?php if ($color1){
echo $color1;}
elseif ($color2){
echo $color2;}
?>
</span>

I also tried these code, but same result. Color2 is always ignored. It shows nothing however color1 does not exist.

<span>
<?php
if (!empty($color1)){
echo $color1;}
elseif (!empty($color2)){
echo $color2;}
?>
</span>


<span>
<?php
if (isset($color1)){
echo $color1;}
elseif (isset($color2)){
echo $color2;}
?>
</span>

Result of var_dump($color1) and ($color2) are like these.

Example 1

$color1
object(SimpleXMLElement)#12441 (1) { [0]=> string(3) "RED" } 

$color2
object(SimpleXMLElement)#12439 (0) { }

Example 2

$color1
object(SimpleXMLElement)#12440 (0) { }

$color2
object(SimpleXMLElement)#12441 (1) { [0]=> string(4) "BLUE" } 
0
cwhiro 1 month 0 Answers 11 views 0

Leave an answer