Created
May 15, 2017 18:49
-
-
Save territutich/1f699931de36480e4d9a7d44b9a0c12a to your computer and use it in GitHub Desktop.
Custom RSS feeds in WordPress
http://yoast.com/custom-rss-feeds-wordpress/ Additions per
https://stackoverflow.com/questions/9123531/add-wordpress-featured-image-to-rss-feed/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Template Name: Custom Feed | |
*/ | |
$numposts = 5; | |
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"?>'; | |
?><rss version="2.0"> | |
<channel> | |
<title>Yoast E-mail Update</title> | |
<link>https://yoast.com/</link> | |
<description>The latest blog posts from Yoast.com.</description> | |
<language>en-us</language> | |
<pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate> | |
<lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate> | |
<managingEditor>joost@yoast.com</managingEditor> | |
<?php foreach ($posts as $post) { ?> | |
<item> | |
<title><?php echo get_the_title($post->ID); ?></title> | |
<link><?php echo get_permalink($post->ID); ?></link> | |
<description><?php echo '<![CDATA['.yoast_rss_text_limit($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>'; ?></description> | |
<pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate> | |
<guid><?php echo get_permalink($post->ID); ?></guid> | |
</item> | |
<?php } ?> | |
</channel> | |
</rss> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// change <rss> to | |
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss"> | |
// add featued image as <media> | |
<?php if(get_the_post_thumbnail()): ?> | |
<media:content url="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>" medium="image" /> | |
<?php endif; ?> | |
// add specific size featued image as <media> | |
<?php if(get_the_post_thumbnail()): ?> | |
<media:content url="<?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'imageSize'); echo $image[0]; ?>" medium="image" /> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment