Page template to create RSS campaigns with WordPress and Mailchimp
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 RSS Feed | |
*/ | |
$numposts = 5; | |
$numchars = 300; | |
$thumbformat = 'medium'; | |
$alt_text = 'Klik hier voor de afbeelding...'; | |
function the_excerpt_max_charlength($excerpt, $charlength) { | |
$charlength++; | |
$str = ''; | |
if ( mb_strlen( $excerpt ) > $charlength ) { | |
$subex = mb_substr( $excerpt, 0, $charlength - 5 ); | |
$exwords = explode( ' ', $subex ); | |
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); | |
if ( $excut < 0 ) { | |
$str .= mb_substr( $subex, 0, $excut ); | |
} else { | |
$str .= $subex; | |
} | |
$str .= '...'; | |
} else { | |
$str .= $excerpt; | |
} | |
return $str; | |
} | |
$posts = query_posts(array('posts_per_page' => $numposts)); | |
$lastdate = date(DATE_RSS, strtotime($posts[0]->post_modified_gmt) ); | |
header("Content-Type: application/rss+xml; charset=UTF-8"); | |
echo '<?xml version="1.0"?> | |
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss"> | |
<channel> | |
<title>'.get_option('blogname').' RSS Updates</title> | |
<link>'.get_option('home').'</link> | |
<description>The latest blog posts from '.get_option('blogname').'.</description> | |
<language>'.strtolower(get_bloginfo('language')).'</language> | |
<pubDate>'.$lastdate.'</pubDate> | |
<lastBuildDate>'.$lastdate.'</lastBuildDate> | |
<managingEditor>'.get_option('admin_email').'</managingEditor>'; | |
foreach ($posts as $post) { | |
setup_postdata( $post ); | |
$excerpt = get_the_excerpt(); | |
echo ' | |
<item> | |
<title>'.get_the_title().'</title> | |
<link>'.get_permalink().'</link> | |
<description><![CDATA['.the_excerpt_max_charlength($excerpt, $numchars).']]></description> | |
<pubDate>'.date(DATE_RSS, strtotime(get_the_date())).'</pubDate>'; | |
if (get_the_post_thumbnail()) { | |
$img = wp_get_attachment_image_src(get_post_thumbnail_id(), $thumbformat); | |
echo ' | |
<media:content url="'.$img[0].'" medium="image" description="'.$alt_text.'"/>'; | |
} | |
echo ' | |
<guid>'.get_permalink().'</guid> | |
</item>'; | |
} | |
wp_reset_postdata(); | |
echo ' | |
</channel> | |
</rss>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Place this template file into your theme directory and create a simple page without any content.
Tip! Use Yoast SEO's "noindex" feature and exclude that page from Google.