logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$4
Simple Custom Query CPT Shortcode

Hello,

I need to code a Shortcode function: [testimonial]

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;?>


Any assistance would be much appreciated.

This question has been answered.

West Coast Design Co. | 12/20/11 at 6:03pm Edit

Previous versions of this question: 12/20/11 at 6:09pm | 12/20/11 at 6:10pm | 12/20/11 at 6:10pm

The experts have suggested, on average, a prize of $10 for this question.

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/20/11
    6:12pm
    Luis Abarca says:

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

    Previous versions of this answer: 12/20/11 at 6:12pm

  • avatar
    Last edited:
    12/20/11
    6:16pm
    designchemical says:

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

    Previous versions of this answer: 12/20/11 at 6:16pm

  • avatar
    Last edited:
    12/20/11
    6:15pm
    Mike Van Winkle says:


    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.

This question has expired.



West Coast Design Co., Gabriel Reguly, Ivaylo Draganov, Julio Potier, Francisco Javier Carazo Gil voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.