0

I'm having problems getting my testimonials to display. On page.php I have a testimonials section. So I have a loop that grabs the content for page.php, and then a get_template_part('content', 'testimonials') to get the testimonial loop.

<div class="grid_12 omega clearfix">

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <h1><?php the_title(); ?></h1>
        <p><?php the_content(); ?></p>
        <hr>

    <?php endwhile; else : ?>

        <p>There were no posts :( </p>

    <?php endif; ?>

</div>

<?php get_template_part( 'content', 'testimonials' ); ?>

So my main loop is up there and looks like that above ^^. My testimonials loop looks like this below in content-testimonials.php:

<?php 

    $args = array(
        'post_type' => 'testimonials',
        'posts_per_page' => 1,
        'orderby' => 'rand'
    );

    $the_query = new WP_Query( $args );

?>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="testimonial push_2 grid_10 clearfix">
        <blockquote>&ldquo;<?php the_field( 'testimonial' ); ?>&rdquo;</blockquote>
        <cite>&mdash;<?php the_field( 'name' ); ?></cite>
    </div>

<?php endwhile; else : ?>

    <p>There were no testimonials :( </p>

<?php endif; ?>

I'm still quite new to Wordpress, so I can't see what I'm missing, I've been through the admin area and made sure there were testimonials, edited the custom field to display if the post type is equal to testimonial, I just can't see why it wouldn't load.

I look forward to your responses, thanks in advance!

4

I think you forgot to check if $the_query have posts.

<?php

    $args = array(
        'post_type' => 'testimonials',
        'posts_per_page' => 1,
        'orderby' => 'rand'
    );

    $the_query = new WP_Query( $args );

?>

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <div class="testimonial push_2 grid_10 clearfix">
        <blockquote>&ldquo;<?php the_field( 'testimonial' ); ?>&rdquo;</blockquote>
        <cite>&mdash;<?php the_field( 'name' ); ?></cite>
    </div>

<?php endwhile; else : ?>

    <p>There were no testimonials :( </p>

<?php endif;
wp_reset_postdata(); ?>

And also don't forget to reset postdata if you are running multiple queries on a page.

  • Beautiful! Thankyou my man! Such a pain, missed the simplest thing. Worked like a charm! – Jake Gabb Aug 28 '14 at 11:22
  • Make sure you also reset postdata on each query, if you are running multiple queries on a page. with wp_reset_postdata(); I updated my answer with this. – Robert hue Aug 28 '14 at 11:24
  • @JakeGabb don't forget to accept this answer :-). For info, check out the tour page – Pieter Goosen Aug 28 '14 at 11:24
  • 1
    I didn't forget ;), but it said I have to wait 9 minutes :'( – Jake Gabb Aug 28 '14 at 11:25
  • @JakeGabb o yes, new user restictions, forgot about that, sorry. BTW Welcome to WPSE. – Pieter Goosen Aug 28 '14 at 11:48

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.