I’m making a shortcode which fetches RSS data, and then the shortcode attribute should fetch a specific item from the feed. So the shortcode will be for example:
[comic id=”2″]
And here’s my code:
function comic_post($atts) {
$default = array(
‘id’ => ‘#’,
);
$rss = simplexml_load_file(‘https://xkcd.com/rss.xml’);
$a = shortcode_atts($default, $atts);
$val = $a[‘id’];
$title = $rss->channel->item[$val]->title;
$link = $rss->channel->item[$val]->link;
echo $title;
}
add_shortcode( ‘comic’, ‘comic_post’ );
Any idea where I’m going wrong? When the id is 0 it returns 0, when the id is anything else, nothing shows up.
[ad_2]