Hello,
I need to code a Shortcode function: <strong>[testimonial]</strong>
From this custom query:
<?php query_posts(array('post_type' => 'testimonials', 'posts_per_page' => 1)); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(''); ?>
<?php endwhile;?>
<em>Any assistance would be much appreciated.</em>
Luis Abarca answers:
Try this code
function testimonial_func($atts)
{
$buffer = '';
$wpq = new WP_Query(array('post_type' => 'testimonials'));
while ( $wpq->have_posts() ) {
$wpq->the_post();
$buffer .= get_the_content();
}
return $buffer;
}
add_shortcode('testimonial', 'testimonial_func');
designchemical answers:
function testimonial_shortcode() {
query_posts(array('post_type' => 'testimonials', 'posts_per_page' => 1));
while ( have_posts() ) : the_post();
$out = get_the_content();
endwhile;
return $out;
}
add_shortcode('testimonial', 'testimonial_shortcode');
Edit - Luis is correct - should be get_the_content()
Mike Van Winkle answers:
function my_custom_shortcode_handler() {
$out = '';
$t = new WP_Query(array('post_type' => 'testimonials'));
ob_start();
while ( $t->have_posts() ) :$t->the_post();
echo $t->post_content;
endwhile;
$out = ob_get_contents();
ob_end_clean();
return $out;
}
add_shortcode('my_custom_shortcode','my_custom_shortcode_handler');
Something like that should work. All that "ob_start" stuff is to make sure the placement of the outputted data is in the right spot.