Support » Fixing WordPress » Custom RSS Template – Blank File Downloads

  • Hi,

    I am trying to create a custom feed template that will not have the excerpt in. I’ve added the following into my Functions file:

    add_action( 'after_setup_theme', 'my_rss_template' );
    /**
     * Register custom RSS template.
     */
    function my_rss_template() {
    	add_feed( 'short', 'my_custom_rss_render' );
    }
    
    /**
     * Custom RSS template callback.
     */
    function my_custom_rss_render() {
    	get_template_part( 'feed', 'short' );
    }

    Then uploaded a new php file with the following included:

    <?php
    /**
     * Customs RSS template with related posts.
     *
     * Place this file in your theme's directory.
     *
     * @package sometheme
     * @subpackage theme
     */
    
    /**
     * Get related posts.
     */
    function my_rss_related() {
    
    	global $post;
    
    	// Setup post data
    	$pid     = $post->ID;
    	$tags    = wp_get_post_tags( $pid );
    	$tag_ids = array();
    
    	// Loop through post tags
    	foreach ( $tags as $individual_tag ) {
    		$tag_ids[] = $individual_tag->term_id;
    	}
    
    	// Execute WP_Query
    	$related_by_tag = new WP_Query( array(
    		'tag__in'          => $tag_ids,
    		'post__not_in'     => array( $pid ),
    		'posts_per_page'   => 3,
    	) );
    
    	// Loop through posts and build HTML
    	if ( $related_by_tag->have_posts() ) :
    
    		echo 'Related:<br />';
    
    			while ( $related_by_tag->have_posts() ) : $related_by_tag->the_post();
    				echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a><br />';
    			endwhile;
    
    		else :
    			echo '';
    	endif;
    
    	wp_reset_postdata();
    }
    
    /**
     * Feed defaults.
     */
    header( 'Content-Type: ' . feed_content_type( 'rss-http' ) . '; charset=' . get_option( 'blog_charset' ), true );
    $frequency  = 1;        // Default '1'. The frequency of RSS updates within the update period.
    $duration   = 'hourly'; // Default 'hourly'. Accepts 'hourly', 'daily', 'weekly', 'monthly', 'yearly'.
    $postlink   = '<br /><a href="' . get_permalink() . '">See the rest of the story at mysite.com</a><br /><br />';
    $postimages = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'large' );
    
    // Check for images
    if ( $postimages ) {
    
    	// Get featured image
    	$postimage = $postimages[0];
    
    } else {
    
    	// Fallback to a default
    	$postimage = get_stylesheet_directory_uri() . '/images/default.jpg';
    }
    
    /**
     * Start RSS feed.
     */
    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' ); ?>
    >
    
      <!-- RSS feed defaults -->
    	<channel>
    		<title><?php bloginfo_rss( 'name' ); wp_title_rss(); ?></title>
    		<link><?php bloginfo_rss( 'url' ) ?></link>
    		<description><?php bloginfo_rss( 'description' ) ?></description>
    		<lastBuildDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_lastpostmodified( 'GMT' ), false ); ?></lastBuildDate>
    		<language><?php bloginfo_rss( 'language' ); ?></language>
    		<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', $duration ); ?></sy:updatePeriod>
    		<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', $frequency ); ?></sy:updateFrequency>
    		<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    
    				<title>
    				<?php bloginfo_rss( 'description' ) ?>
    			</title>
    			<link><?php bloginfo_rss( 'url' ) ?></link>
    
    		<?php do_action( 'rss2_head' ); ?>
    
    		<!-- Start loop -->
    		<?php while( have_posts()) : the_post(); ?>
    
    			<item>
    				<title><?php the_title_rss(); ?></title>
    				<link><?php the_permalink_rss(); ?></link>
    				<guid isPermaLink="false"><?php the_guid(); ?></guid>
    				<author><?php the_author(); ?></author>
    				<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
    			</item>
    
    		<?php endwhile; ?>
    	</channel>
    </rss>

    and finally re-saved my Permalinks. When I go to the feed URL, http://www.thegamersdrop.com/?feed=short for my site The Gamers Drop it just downloads a blank file called “download”.

    Any ideas on where I am going wrong. I was expecting to be able to view an XML file online similar to this one.

    Any help is greatly appreciated

    Regards
    David

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom RSS Template – Blank File Downloads’ is closed to new replies.