首页 文章

在rss feed中调用php函数

提问于
浏览
1

我有一个wordpress网站,我正在创建自己的RSS Feed . 我希望它每天更新 . 我有一个php函数,它在functions.php中设置feed的内容:

function get_daily_Fun_Fact(){
//checking if it is special day today - holiday/cat/dog days
$today=getdate(); 
$weekDay=$today['wday'];
$todays_date=date("Y-m-d");
$holiday_id = isHoliday($todays_date);

// check if today is a special day
$query = create_query($holiday_id,$weekDay);
$select_result = $wpdb->get_results($query);
$num_rows = $wpdb->num_rows;
if($num_rows!=NULL)
{
    foreach($select_result as $row){
    $fact = $row->fact; 
    $fact_id = $row->fact_id;
}   
echo $fact;
}

然后我有一个带有xml代码的模板文件,我想调用函数get_daily_Fun_Fact();所以每天都会更新一个新的$ fact .

<?php
header('Content-Type: '.feed_content_type('rss-http').'; charset='.get_option('blog_charset'), true);
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
?>
<rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        <?php do_action('rss2_ns'); ?>>
<channel>
        <title>The Pet Wiki</title>
        <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
        <link><?php bloginfo_rss('url') ?></link>
        <description>Animal Fun Facts</description>
        <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
        <language><?php echo get_option('rss_language'); ?></language>
        <sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'daily' ); ?></sy:updatePeriod>
        <sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
        <?php do_action('rss2_head'); ?>
        <item>
            <title>
                <?php get_daily_Fun_Fact(); ?>
             </title>
                        <?php rss_enclosure(); ?>
                        <?php do_action('rss2_item'); ?>
        </item>

</channel>
</rss>

但是 get_daily_Fun_Fact(); 发出错误此页面包含以下错误: error on line 21 at column 17: Extra content at the end of the document 如何在rss提要中调用php函数并每天更新?

1 回答

  • 0

    RSS提要是XML . 如果要在不确保数据是有效XML的情况下在提要中包含字符数据,可以将其括在CDATA部分中 .

    如果 get_daily_fun_fact() 返回HTML输出,则可以将其包含在具有以下格式的RSS元素中:

    <title>
      <![CDATA[<?php get_daily_Fun_Fact(); ?>]]>
    </title>
    

    CDATA部分以 <![CDATA[** 开头,以 **]]> 结尾 . 假设它们之间的所有内容都是字符数据而不是XML标记 .

相关问题