Skip Navigation

[Résolu] Shortcode for Custom Post Type in Sidebar not quite working

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 4 réponses, has 2 voix.

Last updated by Eric Il y a 8 années et 6 mois.

Assigned support staff: Adriano.

Auteur
Publications
#67269

I have several custom types. I'd like to display a couple recent posts from each type in the sidebar.

Here's what I have so far. It almost works, but instead of displaying 3 posts, it only displays 1. Is there something wrong with my foreach?


	add_shortcode('sc_sidebar_guest_reviews', 'get_sidebar_guest_reviews');
	
	function get_sidebar_guest_reviews() {
		
		global $post;
		$args = array('numberposts' => 3, 'post_type' => 'guest-reviews', 'orderby' => 'desc');
		$myposts = get_posts($args);

		foreach( $myposts as $post ) : setup_postdata($post);
		
			$display = '';
			$date = types_render_field("date", array("raw"=>"true"));
			$review = types_render_field("review", array("raw"=>"true"));
			$byline = types_render_field("byline", array("raw"=>"false"));
			
			// truncate long reviews
			$max_words = 18;
			$review_words = explode(' ', $review);
			if(count($review_words) > $max_words && $max_words > 0) {
				$review = implode(' ',array_slice($review_words, 0, $max_words)).'&nbsp;<a class="small" href="' .get_permalink() . '">[&hellip;]</a>';
			}
			
			$display = "<div class='date'>$date</div>
						<div class='review'>$review</div>
						<div class='byline'>$byline</div>";
			
		endforeach;
		
		return $display;
				
	}

BTW, in my sidebar, I'm using a simple Text widget with the [sc_sidebar_guest_reviews] shortcode.

Thanks,
Erc

#67315

Dear Eric,

I think that your foreach is right. What happens if you remove the numberposts parameter?

#67335

Hi Adriano,

No change after removing numberposts param. Still showing 1 post.

#67679

Dear Eric,

Please replace your code with:

add_shortcode('sc_sidebar_guest_reviews', 'get_sidebar_guest_reviews');

function get_sidebar_guest_reviews() {

global $post;
$args = array('numberposts' => 3, 'post_type' => 'guest-reviews', 'orderby' => 'desc');
get_posts($args);

while ( have_posts() ) : the_post();

$display = '';
$date = types_render_field("date", array("raw"=>"true"));
$review = types_render_field("review", array("raw"=>"true"));
$byline = types_render_field("byline", array("raw"=>"false"));

// truncate long reviews
$max_words = 18;
$review_words = explode(' ', $review);
if(count($review_words) > $max_words && $max_words > 0) {
$review = implode(' ',array_slice($review_words, 0, $max_words)).' <a class="small" href="' .get_permalink() . '">[…]</a>';
}

$display = "<div class='date'>$date</div>
<div class='review'>$review</div>
<div class='byline'>$byline</div>";

endwhile;

return $display;

}
#68198

The 'while' loop didn't work. I rewrote the function, and for whatever reason it works now. Not sure what I did wrong initially. Here's what I have now:


	// Shortcode to display posts from Guest Reviews cpt in sidebar	
	
	add_shortcode('sc_sidebar_guest_reviews', 'get_sidebar_guest_reviews');

	function get_sidebar_guest_reviews() {
	
		global $post;
		$args = array('numberposts' => 2, 'post_type' => 'guest-reviews', 'orderby' => 'desc');
		$myposts = get_posts($args);
		
		foreach( $myposts as $post ) : setup_postdata($post);
			$review = types_render_field("review", array("raw"=>"true"));
			$byline = types_render_field("byline", array("raw"=>"false"));
			
			if(!isset($byline[0])) { // if the byline is empty, use "Verified Guest"
				$byline = 'Verified Guest';
			}
			
			// truncate long reviews
			$max_words = 18;
			$review_words = explode(' ', $review);
			if(count($review_words) > $max_words && $max_words > 0) {
			$review = implode(' ',array_slice($review_words, 0, $max_words)).' <a class="small" href="' .get_permalink() . '">[...]</a>';
			}
		
		
			$display.=	'<div class="post-container">
							<div class="post-info">
								<div class="date">' . types_render_field("date", array("raw"=>"true")) . '</div>
								<div class="excerpt">"' . $review . '"</div>
								<div class="byline">&mdash;' . $byline . '</div>
							</div>
						</div>';
		
		endforeach;
		
		$display.='<a class="more" href="/guest-reviews" title="blog">More Reviews &raquo;</a>';
		
		return $display;
	
	}