Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Simple Custom Query CPT Shortcode WordPress

  • SOLVED

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>

Answers (3)

2011-12-20

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');

2011-12-20

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()

2011-12-20

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.