Having problems making a shortcode with attributes

[ad_1]

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]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer