• I’m running into a problem with image paths when trying to pull posts from another site on a multisite installation, and I’m hoping that someone here can help. I feel like I’ve gone through more pages of the codex, forum threads, and StackOverflow questions, and keep thinking “surely this can’t be this hard?” Hopefully someone can illuminate what I’m missing.

    I have a multisite network, one of which is a news blog for my entire organization. On another site, for a special project of the organization, I want to pull in news items about that project – which for simplicity’s sake are going to be published on the main news blog.

    Using the switch_to_blog() function, I’ve successfully been able to pull posts from the news blog and present them on the project blog – except for the featured images. Those are showing up broken, with URLs built for the project site rather than the news site.

    What am I missing? Here’s the relevant code:

    <?php
    // switch to news blog
    switch_to_blog(7);
    
    $args = array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'posts_per_page' => 5,
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'tag' => 'foobar'
    	);
    
    $the_slides = null;
    $the_slides = new WP_Query($args);
    
    if( $the_slides->have_posts() ) {
    	while ( $the_slides->have_posts() ) : $the_slides->the_post();
    		setup_postdata($post);
    		?>
    		<p>
    			<a href="<?php the_permalink(); ?>">
    				<?php the_post_thumbnail( array( 1000, 315), array( 'class' => 'slideshow-img' ) ); ?><br>
    				<?php the_title(); ?>
    			</a><br>
    			<?php the_time('F jS, Y'); ?><br>
    		</p>
    
    		<?php
    
    	endwhile;
    }
    
    // switch back to Foobar site
    restore_current_blog();
    ?>

    The call to the_post_thumbnail() is currently returning a path of:

    http://www.example.org/foobar/wp-content/uploads/2012/02/filename.jpg

    … when the image is actually at

    http://www.example.org/news/files/2012/02/filename.jpg

    Among the things I’ve tried are media_sideload_image() (which seems problematic to require the admin templates) and wp_get_attachment_url().

    I’ve thought about doing a str_replace on the returned image path, but that seems clunky in the extreme. Is there a reason that the_post_thumbnail() isn’t apparently respecting the context assigned by switch_to_blog()?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Thread Starter Matt Bernhardt

    (@morphosis7)

    If I understand the linked bug report correctly, this is a known issue with the switch_to_blog() function, which might be fixed in the 3.9 release? I think? I’m not well versed in the internals of WordPress so I may be missing something.

    At any rate, I’ve managed to get the code block working now by abandoning the_post_thumbnail() in favor of a custom-rewritten image tag. Here’s the code:

    <?php
    // switch to news blog
    switch_to_blog(7);
    
    $args = array(
    	'post_type' => 'post',
    	'post_status' => 'publish',
    	'posts_per_page' => 5,
    	'orderby' => 'post_date',
    	'order' => 'DESC',
    	'tag' => 'foobar'
    	);
    
    $the_slides = null;
    $the_slides = new WP_Query($args);
    
    if( $the_slides->have_posts() ) {
    	while ( $the_slides->have_posts() ) : $the_slides->the_post();
    		setup_postdata($post);
    
    		$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumbnail');
    		$imageURL = str_replace('/foobar/wp-content/uploads/','/news/files/',$image[0]);
    		$imageTag = '<img src="'.$imageURL.'" alt="" height="'.$image[2].'" width="'.$image[1].'">';
    		?>
    		<p>
    			<a href="<?php the_permalink(); ?>">
    				<?php echo $imageTag; ?><br>
    				<?php the_title(); ?>
    			</a><br>
    			<?php the_time('F jS, Y'); ?><br>
    		</p>
    
    		<?php
    
    	endwhile;
    }
    
    // switch back to Foobar site
    restore_current_blog();
    ?>

    Hopefully this helps save someone else some trouble.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘post thumbnails for another site appearing wrong after switch_to_blog()’ is closed to new replies.