希望写一篇题为《核电相关的美国上市公司及ETF》的日志中,在日志中列出公司名称及简单介绍之后,再调用Yahoo财经中关于该公司的头条消息。
ps:关于股票代码gvp(公司名称为GSE SYSTEMS, INC.)头条消息的Yahoo财经API是 http://finance.yahoo.com/rss/headline?s=gvp
经过网络查询和比较之后,发现通过wordpress简码实现比较合适:
在function.php中插入以下代码:
//This file is needed to be able to use the wp_rss() function.
include_once(ABSPATH.WPINC.'/rss.php');
function readRss($atts) {
extract(shortcode_atts(array(
"feed" => 'http://',
"num" => '1',
), $atts));
return wp_rss($feed, $num);
}
add_shortcode('rss', 'readRss');
通过html格式撰写日志,插入以下代码即可:
[rss feed="http://finance.yahoo.com/rss/headline?s=gvp" num="5"]
遇到的问题是列出多个公司的头条消息时所有的rss总在日志的最上面,即使使用以下的格式也仍然是这样:
<div>
Entergy Corp.
[rss feed="http://finance.yahoo.com/rss/headline?s=etr" num="5"]
</div>
<hr>
<div>
GSE SYSTEMS, INC.
[rss feed="http://finance.yahoo.com/rss/headline?s=gvp" num="5"]
</div>
通过进一步google,发现需要对简码作以下优化:
//This file is needed to be able to use the wp_rss() function.
include_once(ABSPATH.WPINC.'/rss.php');
function readRss($atts) {
extract(shortcode_atts(array(
"feed" => 'http://',
"num" => '1',
), $atts));
ob_start();
wp_rss($feed, $num);
return ob_get_clean();
}
add_shortcode('rss', 'readRss');
最终的效果请参见:http://www.anpc.cn/u-s-stocks-associated-with-the-nuclear-power/
本文参考了:http://finance.yahoo.com/rssindex
我自己的原创:http://www.zxxh.cn