3
votes

I have a custom post type testimonial and am trying to display the title, content and the featured image.

This is my code:

    <?php
$args = array('posts_per_page' => -1, 'post_status' => 'publish', 'post_type' => 'testimonial');
$testimonialsposts = get_posts($args);
?>
<div class="row">
    <?php
    foreach ($testimonialsposts as $post)
    {

    setup_postdata($post);
    ?>

    <div class="testimonial-box projectitem">
        <img src="<?= get_post_meta($post->ID, '_wp_attached_file', true); ?>"/>
    </div>
    <div class="testimonial-contact">
        <div class="testimonial-text"><?= get_post($post->ID, 'content', true); ?></div>
        <div class="testimonial-name"><?= get_post($post->ID, 'post_title', true); ?></div>
        <?php wp_reset_postdata();
        } ?>
    </div>

But I am getting nothing. When I tried to echo the details after setup_postdata its only echoing "array".

When I var_dump the $post it shows all the data also.

This is what i get when I var_dump $post

object(WP_Post)#614 (24) { ["ID"]=> int(69) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2017-08-22 02:26:43" ["post_date_gmt"]=> string(19) "2017-08-22 02:26:43" ["post_content"]=> string(575) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." ["post_title"]=> string(13) "testimonial_1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(11) "testimonial" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2017-08-22 02:27:07" ["post_modified_gmt"]=> string(19) "2017-08-22 02:27:07" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(63) "http://192.168.0.202/clinic202/?post_type=testimonial&p=69" ["menu_order"]=> int(0) ["post_type"]=> string(11) "testimonial" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

What am I doing wrong?

1
You can use print_r() instead of echo() to display the contents of an array. If that doesn't help, please update your post to say if the array was empty or what it containedFluffyKitten
@FluffyKitten i have updated my qnJackson
You didn't need to post the whole lot, just let us know it was working :-) OK, so your query is working, but you loop is all wrong - you are using get_post for some reason instead of just using get_the_title or get_the_content, and you are not closing your testimonial-contact div in the correct place.FluffyKitten

1 Answers

1
votes

I understand you are using get_posts but this is what i would do, query the posts and loop by using wordpress loop instead using foreach.

try this

<?php
    query_posts(array(
        'post_type' => 'testimonial',
        'posts_per_page' => -1,
    ));
    if (have_posts()) : while (have_posts()) : the_post(); ?>
     <div class="testimonial-box projectitem">
         <?php echo wp_get_attachment_image( get_post_thumbnail_id( $post->ID ), '', 'false', array( "class" => "img-responsive" ) ); ?>
     </div>
    <div class="testimonial-contact">
        <div class="testimonial-text"><?php the_content();?></div>
        <div class="testimonial-name"><?php the_title(); ?></div>

    </div>
<?php endwhile; endif;
      wp_reset_postdata();
      wp_reset_query(); ?>

also you are closing foreach inside <div> which will break your html code.