Adding Site Name and Featured Image to Global Posts Feed

I got help from Paul writing a couple queries and to get the site name and featured image, but for some reason they are not displaying within the feed content. I did a bit of clean up to close out the query, but am not quite sure how to get them to show up. Here's my loop, anyone have any guidance?

if ( count( $posts ) > 0 ) {
		foreach ($posts as $post) {
			$author_display_name = $wpdb->get_var("SELECT display_name FROM " . $wpdb->base_prefix . "users WHERE ID = '" . $post['post_author'] . "'");
			$blog_name = get_blog_option($post['blog_id'], ‘blogname’);
			switch_to_blog( $post['blog_id'] );
			$featured_image_id = get_post_meta($post['ID'], ‘_thumbnail_id’, true);
			if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}
			?>
			<item>
				<title><?php echo apply_filters( 'the_title_rss', $post['post_title']); ?></title>
				<link><?php echo $post['post_permalink']; ?></link>
				<comments><?php echo $post['post_permalink'] . '#comments'; ?></comments>
				<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', $post['post_published_gmt'], false); ?></pubDate>
				<dc:creator><?php echo $author_display_name; ?></dc:creator>
				<guid isPermaLink="false"><?php echo $post['post_permalink']; ?></guid>
                <description><![CDATA[<?php echo apply_filters('the_excerpt_rss', wp_trim_excerpt($post['post_content'])); ?>]]></description>
                <content:encoded><![CDATA[<?php echo $blog_name; ?><?php echo $image_src; ?><?php echo apply_filters('the_content_feed', $post['post_content'], 'rss2'); ?>]]></content:encoded>
				<wfw:commentRss><?php echo $post['post_permalink'] . 'feed/'; ?></wfw:commentRss>
			</item>
			<?php
		}
	}
  • jakelisby

    Ok, that got the blog name to show up in there, but I'm still not getting the featured image.

    I'm not getting any errors either, and checking the logs of the server, there's no issues. It's just not showing for some reason...

    Thanks for your help so far, we're so close...

    if ( count( $posts ) > 0 ) {
    		foreach ($posts as $post) {
    			$author_display_name = $wpdb->get_var("SELECT display_name FROM " . $wpdb->base_prefix . "users WHERE ID = '" . $post['post_author'] . "'");
    			$blog_name = get_blog_option(get_current_blog_id(), 'blogname');
    			switch_to_blog( $post['blog_id'] );
    			$featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    			if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}
    			?>
    			<item>
    				<title><?php echo apply_filters( 'the_title_rss', $post['post_title']); ?></title>
    				<link><?php echo $post['post_permalink']; ?></link>
    				<comments><?php echo $post['post_permalink'] . '#comments'; ?></comments>
    				<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', $post['post_published_gmt'], false); ?></pubDate>
    				<dc:creator><?php echo $author_display_name; ?></dc:creator>
    				<guid isPermaLink="false"><?php echo $post['post_permalink']; ?></guid>
                    <description><![CDATA[<?php echo apply_filters('the_excerpt_rss', wp_trim_excerpt($post['post_content'])); ?>]]></description>
                    <content:encoded><![CDATA[<span id="schoolName"><?php echo $blog_name; ?></span> <?php echo $image_src; ?> <?php echo apply_filters('the_content_feed', $post['post_content'], 'rss2'); ?>]]></content:encoded>
    				<wfw:commentRss><?php echo $post['post_permalink'] . 'feed/'; ?></wfw:commentRss>
    			</item>
    			<?php
    		}
    	}
  • jakelisby

    Apparently it doesn't want me to post that file type... Sorry, but here is all the code...

    <?php
    /*
    Plugin Name: Recent Global Posts Feed
    Plugin URI:
    Description: RSS2 feed showing global posts - to access feed go to http://yoursite.com/feed/globalpostsfeed
    Version: 3.0 RC 1
    Author: Barry (Incsub)
    Author URI:
    WDP ID: 70
    Network: true
    */
    
    /*
    Copyright 2012 Incsub (http://incsub.com)
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
    the Free Software Foundation.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    */
    
    class globalpostsfeed {
    
    	var $build = 1;
    
    	var $db;
    
    	function __construct() {
    
    		global $wpdb;
    
    		$this->db =& $wpdb;
    
    		if($this->db->blogid == 1) {
    			// Only add the feed for the main site
    			add_action('init', array(&$this, 'initialise_global_posts_feed') );
    		}
    
    	}
    
    	function globalpostsfeed() {
    		$this->__construct();
    	}
    
    	function initialise_global_posts_feed() {
    
    		global $wp_rewrite;
    
    		$installed = get_option('globalpostsfeed_version', false);
    
    		add_feed('globalpostsfeed', array( &$this, 'do_global_posts_feed' ));
    
    		if($installed === false || $installed < $this->build) {
    			// We need to flush our rewrites so that the new feed is added and recognised
    			flush_rewrite_rules();
    			update_option('globalpostsfeed_version', $this->build);
    		}
    
    	}
    
    	function do_global_posts_feed() {
    
    		global $network_query, $network_post;
    
    		// Remove all excerpt more filters
    		remove_all_filters('excerpt_more');
    
    		@header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
    		$more = 1;
    
    		echo '<?xml version="1.0" encoding="' . get_option('blog_charset') . '"?' . '>';
    
    		$number = $_GET['number'];
    		if ( empty( $number ) ) {
    			$number = '25';
    		}
    
    		$posttype = $_GET['posttype'];
    		if(empty($posttype)) {
    			$posttype = 'post';
    		}
    
    		$network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $number ));
    		$blog_name = get_blog_option(get_current_blog_id(), 'blogname');
    		switch_to_blog( $post['blog_id'] );
    		$featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    		if ($featured_image_id) {$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));}
    
    		?>
    		<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'); ?>
    		>
    
    		<channel>
    			<title><?php bloginfo_rss('name'); _e(' - Recent Global Posts','postindexer'); ?></title>
    			<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    			<link><?php bloginfo_rss('url') ?></link>
    			<description><?php bloginfo_rss("description") ?></description>
    			<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', network_get_lastpostmodified('GMT'), false); ?></lastBuildDate>
    			<language><?php bloginfo_rss( 'language' ); ?></language>
    			<sy:updatePeriod><?php echo apply_filters( 'rss_update_period', 'hourly' ); ?></sy:updatePeriod>
    			<sy:updateFrequency><?php echo apply_filters( 'rss_update_frequency', '1' ); ?></sy:updateFrequency>
    			<?php do_action('rss2_head'); ?>
    			<?php while( network_have_posts()) : network_the_post(); ?>
    			<item>
    				<title><?php network_the_title_rss(); ?></title>
    				<link><?php network_the_permalink_rss(); ?></link>
    				<comments><?php network_comments_link_feed(); ?></comments>
    				<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', network_get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
    				<dc:creator><?php network_the_author(); ?></dc:creator>
    				<?php network_the_category_rss('rss2'); ?>
    
    				<guid isPermaLink="false"><?php network_the_guid(); ?></guid>
    		<?php if (get_option('rss_use_excerpt')) { ?>
    				<description><![CDATA[<?php network_the_excerpt_rss(); ?>]]></description>
    		<?php } else { ?>
    				<description><![CDATA[<?php network_the_excerpt_rss() ?>]]></description>
    			<?php if ( strlen( $network_post->post_content ) > 0 ) { ?>
    				<content:encoded><![CDATA[schoolName="<?php echo $blog_name; ?>" <?php echo $image_src; ?> <?php network_the_content_feed('rss2'); ?>]]></content:encoded>
    			<?php } else { ?>
    				<content:encoded><![CDATA[schoolName="<?php echo $blog_name; ?>" <?php echo $image_src; ?> <?php network_the_excerpt_rss(); ?>]]></content:encoded>
    			<?php } ?>
    		<?php } ?>
    				<wfw:commentRss><?php echo esc_url( network_get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
    				<slash:comments><?php echo network_get_comments_number(); ?></slash:comments>
    		<?php network_rss_enclosure(); ?>
    			<?php do_action('network_rss2_item'); ?>
    			</item>
    			<?php endwhile; ?>
    		</channel>
    		</rss>
    		<?php
    	}
    
    }
    
    $globalpostsfeed = new globalpostsfeed();
    
    ?>
  • Barry

    This bit:

    switch_to_blog( $post['blog_id'] );
    $featured_image_id = get_post_meta(get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {
    $image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    }

    Should become something like:

    switch_to_blog( $network_post->BLOG_ID );
    $featured_image_id = get_post_meta(network_get_the_ID(), ‘_thumbnail_id’, true);
    if ($featured_image_id) {
    $image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    }
    restore_current_blog();

    Note: I just typed these into the forum post, so I haven't checked for syntax errors - prob best to give them a quick scan first :slight_smile:

  • jakelisby

    So here's the query portion as I have it now. At this time it's still displaying the root site's Blog Name and not showing the Image src in the feed. All I'm really needing is the source url, so if I can get that in there, I'll be good.

    $network_query = network_query_posts( array( 'post_type' => $posttype, 'posts_per_page' => $number ));
    		$blog_name = get_blog_option( $network_post->BLOG_ID, 'blogname');
    		switch_to_blog( $network_post->BLOG_ID );
    		$featured_image_id = get_post_meta($network_post->ID, ‘_thumbnail_id’, true);
    		if ($featured_image_id) {
    			$image_src = wp_get_attachment_image($featured_image_id, array(100, 100));
    		}
    		restore_current_blog();
  • Imperative Ideas

    That's a long way to go for a ham sandwich.

    Add this to your functions.php if you want the post thumbnail to show up in your RSS feed as part of the site description:

    /* include thumbnail in RSS feed */
    function add_thumb_to_RSS($content) {
       global $post;
       if ( has_post_thumbnail( $post->ID ) ){
          $content = '' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '' . $content;
       }
       return $content;
    }
    add_filter('the_excerpt_rss', 'add_thumb_to_RSS');
    add_filter('the_content_feed', 'add_thumb_to_RSS');
  • jakelisby

    Ok, we have this finished, thanks for all the help along the way.

    Replace your network_have_posts single line with this:

    <?php while( network_have_posts()) : network_the_post();
    				switch_to_blog($network_post->BLOG_ID);         
    
    		        //Get network blog post featured image
    		        $featured_image_id = get_post_thumbnail_id(network_get_the_ID());
    		        $featured_image_src = '';
    		        if ($featured_image_id) {
    		            $featured_image_src = array_shift(wp_get_attachment_image_src($featured_image_id, 'thumbnail'));
    		        }     
    
    		        //Network blog name
    		        $blogname = get_blog_option($network_post->BLOG_ID, 'blogname');
    
    		        restore_current_blog();
    		    ?>

    Then you can use this to output the variables in your feed:
    <?php echo $blogname; ?>
    and
    <?php echo $featured_image_src; ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.