Search code examples
phpwordpresscustom-post-type

how to show all testimonial on single page via shortcode in wordpress?


i have created shortcode to show all testimonial on one page. i have created custom post type to add each testimonial. can anybody tell me how to show all testimonial on single page . this code is used to create sshortcode below i m mentioning code :-

function fn_testimonials_block()
{
$args = array(
    'post_type'=> 'testimonials',
    'order'    => 'ASC'
    );              

$the_query = new WP_Query( $args );

$pages              = $the_query->posts;

//echo "<pre>";
//print_r($pages);
//echo "</pre>";

//exit;

$output = '';
$count = 1;

foreach($pages as $page) {

    //-----------------

    $author         = $page->post_title;
    $testimonial    = $page->post_content;
    $page_url       = get_page_link( $page->ID );
    $author_image   = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );

    if ($count%2 == 1)
    {  
         $output .= '<div>';
    } 

    $output .= '<div>
              <p>'.$testimonial.'<br>
              <img src="'.$author_image .'"> <span>'.$author.', </span></p>
            </div>';

    if ($count%2 == 0) 
    {
        $output .= '</div>';
    }

    $count++;

}

if ($count%4 != 1){
    $output .= '</div>';
} 

return $output;
}

Solution

  • I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this

    $args = array(
        'post_type'=> 'testimonials',
        'order'    => 'ASC',
        'posts_per_page' => '-1'
    );
    

    I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.

    Making changes in $args will return all testimonials objects.