0

I'm trying to use a shortcode to call one post from a custom post type and display it inside another page's content. (Basically, we have a custom post type called 'testimonials' and we want to display a random testimonial on each page, but inside the page's content.) I used this tutorial as my starting point.

Here's my code:

function cd_random_testimonial($atts) {

   // EXAMPLE USAGE:
   //[loop the_query="showposts=1&post_type=testimonials&orderby=rand"]

   // Defaults
   extract(shortcode_atts(array(
  "the_query" => ''
   ), $atts));

   // de-funkify query
   $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
   $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

   // query is made  
   query_posts($the_query);

   // Reset and setup variables
   $output = '';

   // the loop
   if (have_posts()) : while (have_posts()) : the_post();

      $testimonial = the_content($post->ID);
      $clientname = get_the_title($post->ID); 

      // output all findings - CUSTOMIZE TO YOUR LIKING
      $output .= '<blockquote><p class="quote">';
      $output .= get_the_content($post->ID);
      $output .= '</p><p class="right">~ ';
      $output .= $clientname;
      $output .= '</p></blockquote>';

   endwhile; else:

      $output .= 'nothing found.';

   endif;
   wp_reset_query();
   return $output;

    }
    add_shortcode("loop", "cd_random_testimonial");

It's working exactly as it should where the shortcode is entered, but it's also adding that same content above the page's content.

How do I keep it from posting that extra content and just stay where it's supposed to?

Thanks!

0

You have few errors in your code. Below you can see how it should look like:

function cd_random_testimonial($atts) {

    // EXAMPLE USAGE:
    // [loop the_query="showposts=1&post_type=testimonials&orderby=rand"]

    // Defaults
    extract(shortcode_atts(array(
        'the_query' => ''
        ), $atts));

    // de-funkify query
    $the_query = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $the_query);
    $the_query = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $the_query);

    // query is made
    $my_query = new WP_Query( $the_query );  // use custom WP_Query instead of query_posts

    // Reset and setup variables
    $output = '';

    // the loop
    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();

            // the_content outputs content and you want to return it; and it doesn't take post_id as argument... and you don't use this variable, so you can delete this line, I guess
            // $testimonial = get_the_content();  
            $clientname = get_the_title(); // it doesn't take post_id as argument

            // output all findings - CUSTOMIZE TO YOUR LIKING
            $output .= '<blockquote><p class="quote">';
            $output .= get_the_content();  // doesn't take argument
            $output .= '</p><p class="right">~ ';
            $output .= $clientname;
            $output .= '</p></blockquote>';
        }
    } else {
        $output = 'nothing found.';
    }
    wp_reset_postdata();
    return $output;
}
add_shortcode("loop", "cd_random_testimonial");
1
  • That fixed it! Thanks. This is what happens when you try to hack a three-year-old tutorial. :) Jul 12 '13 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.