wordpress如何创建自定义的rss

项目中遇到这样一个问题,针对某个分类 输出自定义的rss。

比如要显示分类id为8的rss。

最简单的做法是创建page 模板.

1,创建分类test

2,创建page模板并放到主题目录下面(比如文件名为page-viihdekalenteri-rss.php)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* Template Name: Viihdekalenteri RSS Feed
*/
header("Content-Type: application/xhtml+xml; charset=utf-8");
function rfcDate( $s ) {
//return date(DATE_RFC822, strtotime(get_the_date()));
return date(DATE_RFC822,strtotime($s));
}

$feed_url = 'http://test.com/viihdekalenteri-rss/';
?>
<rss 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/" version="2.0">
<channel>
<title>Kylpylähotelli Levitunturi » Tapahtumakalenteri</title>
<atom:link href="<?php echo $feed_url?>" rel="self" type="application/rss+xml"/>
<link>http://test.com</link>
<description/>
<language>fi</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=3.8.2</generator>
<pubDate><?php echo rfcDate( date('Y-m-d H:i:s', time()) ); ?></pubDate>
<lastBuildDate><?php echo rfcDate( date('Y-m-d H:i:s', time()) ); ?></lastBuildDate>

<?php
// $category="custom-category";

// $args = array(
// 'category_name' => $category,
// 'meta_key' => 'wpcf-start-date',
// 'orderby' => 'meta_value_num',
// 'order' => 'ASC',
// 'offset' => '0',
// 'posts_per_page' =>'9999'
// );
// note that offset and posts_per_page solved an issue of some posts not showing up
$args = array('cat'=>8);
$query = new WP_Query( $args );

while ( $query->have_posts() ) :

$query->the_post();

$post_id = get_the_ID();

$title = get_the_title();
$link = get_permalink();
$pub_date = rfcDate(get_the_date());
$excerpt = get_the_excerpt();
//$meta_values = get_post_meta($post_id);
//var_dump($meta_values);
$end_date = get_post_meta($post_id, 'tapahtuma-alkaa', true);
if(!empty($end_date)){
$now = strtotime($end_date);
$d = new DateTime();
$d->setTimeStamp($now);
$d->setTime(23, 59, 59);
$end_date = $d->format('Y-m-d H:i:s');
}
$theme_image = get_post_meta($post_id, 'theme_image', true);
$nosto_image = get_post_meta($post_id, 'nosto_image', true);
$is_nosto = get_post_meta($post_id, 'is_nosto', true);
//$end_date = isset($meta_values['tapahtuma-alkaa'])?$meta_values['tapahtuma-alkaa']:'';
//$theme_image = isset($meta_values['theme_image'])?$meta_values['theme_image']:'';
//$nosto_image = isset($meta_values['nosto_image'])?$meta_values['nosto_image']:'';
//$is_nosto = isset($meta_values['is_nosto'])?$meta_values['is_nosto']:'';
?>
<item>
<title><?php echo $title; ?></title>
<link><?php echo $link; ?></link>
<description><?php echo $excerpt; ?></description>
<loppupvm><?php echo $end_date?></loppupvm>
<teemakuva><?php echo $theme_image ?></teemakuva>
<nostokuva><?php echo $nosto_image ?></nostokuva>
<nosto><?php echo $is_nosto?></nosto>
<tyyppi>musiikki</tyyppi>
</item>
<?php
endwhile;
?>
</channel>
</rss>

3,后台创建page页面 并选择上面的模板(page-viihdekalenteri-rss.php);

 

4,测试

浏览器访问创建的page页面,就是自定义的xml rss

坚持原创技术分享,您的支持将鼓励我继续创作!