Skip Navigation

[Résolu] Display featured image and caption from recent posts of custom post type

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 3 réponses, has 2 voix.

Last updated by Luo Yang Il y a 8 années et 2 mois.

Assigned support staff: Luo Yang.

Auteur
Publications
#112272

Hello,

I have a custom post type called Impact Stories. Along with the post content, each post requires a featured image with a caption. I need to display 3 of these posts' featured images and captions in a sidebar. I've tried many variations of this code, but am apparently overlooking something.

	add_shortcode('sidebar-posts-featured-img-caption', 'sidebar_posts_featured_img_caption');
	
	function sidebar_posts_featured_img_caption() {
		
		global $post;
		
		$args = array( 
			'post_status' => 'publish', 
			'post_type' => 'impact-stories', 
			'orderby' => 'post_date', 
			'numberposts' => 4 
			); 
		
		$myposts = get_posts($args);

		$display = '';
		foreach( $myposts as $post ) : setup_postdata($post);
			
			$image_id = get_post_thumbnail_id(); 
			$thumbnail_image = get_posts(array('p' => $image_id, 'post_type' => 'attachment'));
			$caption = $thumbnail_image[0]->post_excerpt;
			$display = '<div class="featured-image">
							<a href="'. get_permalink() .'">' . get_the_post_thumbnail($post->ID, 'thumbnail') . '
								<span class="caption-text">' . $caption . '</span>
							</a>
						</div>';
	
		endforeach;
		
		return $display;
		
	}

The code above doesn't display anything in my sidebar. If, however, I change 'post_type' => 'impact-stories' to 'post_type' => 'impact-story', a single post appears in my sidebar, displaying the featured image and caption. I've also tried using a "while" instead of foreach. That method only displays a single post.

#112348

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi Eric,

Please try manually debug your codes line by line:
modify your codes as this:

...
echo $image_id = get_post_thumbnail_id(); 
$thumbnail_image = get_posts(array('p' => $image_id, 'post_type' => 'attachment'));
echo $caption = $thumbnail_image[0]->post_excerpt;
...

check if it output correct thumbnail_image ID and caption.
1) If it does output correct thumbnail_image ID and caption, modify this line
From:

 $display = '<div class="featured-image">

To:

 $display .= '<div class="featured-image">

2) If it doesn't output correct thumbnail_image ID and caption,
Seems there isn't thumbnail image in first four posts, you need add thumbnail image in it.

#112534

Thanks Iuoy!

Here's what fixed it:

Changed:

'post_type' => 'impact-stories',

To:

'post_type' => 'impact-story',

And changed (as you suggested):

$display = '<div class="featured-image">

To:

$display .= '<div class="featured-image">

I'll mark "resolved," but I'm a little confused about when to use the plural name of the custom post type. Can you explain?

Thank you,
Eric

#112787

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

It should be post type slug "impact-story":

http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters