开发者

How to create a NewsML WordPress feed

I'm trying to create a fully custom feed from my WordPress blog so that it can be ingested by a content management system that uses NewsML. You can learn about NewsML and find a NewsML validator here.

My approach has been to create a WP page template that produces xml rather than HTML, described by a guy named Yoast (google him, I can't post too many links b/c I'm new here). However, nothing seems to satisfy the NewsML validator. Does anyone out there have any insights into creating NewsML feeds and/or any experience doing it with WordPress? It seems like NewsML is extremely complicated and tripping me up everywhere I turn.

Here's the template I've come up with to create the feed. Feel free to try it out if you want.

<?php

$numposts = 10;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}

function yoast_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 
}

$posts = query_posts('showposts='.$numposts);
$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?>
<NewsML>
    <Catalog Href="http://www.iptc.org/std/catalog/catalog.IptcMasterCatalog.xml"/>
    <NewsEnvelope>
        <DateAndTime><?php echo mysql2date('Ymd', get_lastpostmodified('GMT'), false); ?>T<?php echo mysql2date('His', get_lastpostmodified('GMT'), false); ?></DateAndTime>
        <NewsService FormalName="Thumbnail" link-url="http://example.com">
            http://example.com/images/example.gif
        </NewsService>
    </NewsEnvelope>


<?php foreach ($posts as $post) { ?>
    <?php setup_postdata($post); ?>

    <NewsItem LinkType="normal">
        <Identification>
            <NewsIdentifier>
                <ProviderId><?php the_author(); ?></ProviderId>
            </NewsIdentifier>
        </Identification>
        <NewsManagement>
            <NewsItemType FormalName="News"/>
            <FirstCreated><?php the_time(Ymd); ?>T<?php the_time(His); ?></FirstCreated>
            <ThisRevisionCreated><?php the_modified_time(Ymd); ?>T<?php the_modified_time(Ymd); ?></ThisRevisionCreated>
            <Status FormalName="Usable"/>
        </NewsManagement>
        <NewsComponent Duid="<?php echo get_permalink($post->ID); ?>copyright" Essential="no" EquivalentsList="no">
            <NewsLines>
                <CopyrightLine>Placeholding</CopyrightLine>
            </NewsLines>
            <NewsComponent Duid="<?php echo get_permalink($post->ID); ?>">
            <DateLine><?php the_time(Ymd); ?>T<?php the_time(His); ?></DateLine>
            <Author><?php the_author(); ?></Author>
                <NewsCo开发者_StackOverflow社区mponent>
                    <Role FormalName="Main"/>
                    <NewsLines>
                        <HeadLine><?php echo get_the_title($post->ID); ?></HeadLine>
                        <SlugLine> 
                            <?php the_excerpt(); ?>
                        </SlugLine>
                        <MoreLink> 
                            <?php echo get_permalink($post->ID); ?>
                        </MoreLink>
                    </NewsLines>
                    <ContentItem link-url="<?php echo get_permalink($post->ID); ?>">
                        <DataContent>
                            <nitf>
                                <body>
                                    <body.head>
                                        <hedline>
                                            <hl1><?php echo get_the_title($post->ID); ?></hl1>
                                        </hedline>
                                    </body.head>
                                    <body.content>
                                        <?php echo apply_filters('the_content',$post->post_content);  ?>
                                    </body.content>
                                </body>
                            </nitf>
                        </DataContent>
                    </ContentItem>
                </NewsComponent>
            </NewsComponent>    
        </NewsComponent>
    </NewsItem>
<?php } ?>
</NewsML>


Resurrecting this from the grave - was looking for exactly this. Not sure how strictly your feed is validated, because I couldn't get the provided link to work, but I was able to get it to validate for my purposes by enclosing body and other potentially-html-including fields in CDATA tags. Thanks for the code, though!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜