Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Jason Broderick
Jason Broderick
7,361 Points

How to build a Wordpress theme - Testimonial content not showing!

Hi Guys,

Trying to apply the lessons in Zac Gordons How To Build A Wordpress Theme series to my own site. Not too good with php yet I've spent two days trying to get this little bit to work.

CPT is set up exactly as in his videos;

Post Type Name - testimonials

ACF same;

Field Names - image , quote , name , featured ,

I have my theme set up to import the content-testimonials.php file in my child theme and it is displaying the header of the section but then nothing underneath that displays. When it is static it works but as soon as I try and make it dynamic it disappears. Can you see anything wrong here?

PS I am trying to import this using a child of a premium theme called AVADA might that be causing conflict?

Thanks!

<div class="testimonial-box"> <div> <h5>Success Story</h5> </div>

<?php 
    $args = array(
        'post_type' => 'testimonials');

    $the_query = new WP_Query( $args );

?>

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

<div class="testimonial">
    <div class="testimonial-image"> <?php the_field( 'image' ); ?></div>
    <div class="testimonial-quote"> <?php the_field( 'quote' ); ?></div>
    <div class="testimonial-name"> <?php the_field ( 'name' ); ?></div>
    <div class="testimonial-featured"> <?php the_field ( 'featured' ); ?></div>
</div>
<?php endwhile; endif; ?>

</div>

5 Answers

Jason Broderick
Jason Broderick
7,361 Points

Solved it!

Putting a <?php wp_reset_query(); ?> in my page.php file before the <?php get_template_part... was what it needed!

because I was using a custom theme it had been called earlier in the page.php and not reset yet!

All good now!

Thanks for all your suggestions!

Ah the reset - good to always check that one.

Jason Broderick
Jason Broderick
7,361 Points

Yea, I'm totally new to wordpress functions and pretty new to php so I literally just guessed and it worked!

Cavan Biggs
Cavan Biggs
25,212 Points

I was having the exact same issue I went back looked over the code, checked the custom post type , custom fields and in the end I entered the testimonial in the title field instead of the testimonial field. MEGA FACEPALM :), hope this helps someone else.

Jason Broderick
Jason Broderick
7,361 Points

Hi Cavan, thanks for weighing in.

Im totally expecting a major facepalm moment at the end of this but could you run me through a little more specific.

Do you mean actually posting the testimonial words in the 'title' field?

As you can see from my example above I am trying to apply the lesson to my own site and am not using the same field names as he uses in the videos - my problem is that none of the dynamic content is displaying. None of it!

My heading is displaying but nothing else, it seams my custom post type isn't loading into my $args?

Have I missed a reset_post_data or some other mystical wordpress query?

Zac Gordon - I would really appreciate your thoughts on this!

Jason Broderick
Jason Broderick
7,361 Points

Update:

I have reverted to using the exact code in the demo video and changed my custom fields back to his exact ones just to be absolutely sure;

I've added some basic php echo statements throughout to see where the code fails and it seams that nothing inside my if statement is executing.

I have it to echo out a number after every line and as soon as my if statement executes the numbers stop and after I endif they start again?

I've tried taking the if while out and I then get every line executing but nothing happens with the_field.

I get $ldquo; $rdquo; and $mdash; which suggests to me that something is also making the html not render?

I am so stumped right now!

Short of solving that can anybody suggest a plugin I can use to manage testimonials for me. One that renders the content with no styling?

In your loop you have a $ on the_post when you shouldn't. It should be a string rather than an object at this point in the loop statement.

Change this:

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

To this:

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Jason Broderick
Jason Broderick
7,361 Points

Hi Jeff,

Thanks for your comment. I have edited that $ out but still nothing is working.

Nothing after my if statement is loading.

This is what my code currently looks like, my CPT is set to 'testimonials', my custom fields have the names 'name', and 'testimonial'.

<div>
    <h5>Success Story</h5>
</div>

<?php 

    $args = array(
        'post_type' => 'testimonials');

    $the_query = new WP_Query( $args );

?>

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

<div class="testimonial">
    <div class="testimonial-name"> <?php the_field ( 'name' ); ?></div>
    <div class="testimonial-quote"> <?php the_field( 'testimonial' ); ?></div>
</div>
<?php endwhile; endif; ?>

I am about one more day short of just paying someone on Elance to fix this for me! Any takers?

Zac Gordon, would love your help on this.

Carole Nissen
Carole Nissen
5,655 Points

Having a v similar problem - I wondered if you ever sorted this Jason? Or anyone else having a similar problem? I don't get the title or the testimonial, just the name of the writer (and a bit of presumably css where the testimonial should be - i.e. a vertical line followed by empty inverted commas). Even when I copy the code from the project files from the next tutorial where they are updated just to be sure there are no errors, it doesn't work. But when I go back to static it works fine.... I guess I'll go right back to setting up the testimonials as a custom post type again & see if the error is there - but it's ****** frustrating!!!! Not loving WP at the moment I have to say!!!!

Jason Broderick
Jason Broderick
7,361 Points

Hi Carole,

For me I wasn't working on the exact code in the tutorial I was trying to implement it on my own new site. As you can see from above all I had to do was reset the wp query by adding <?php wp_reset_query(); ?> in my page.php just before I called the <?php get_template_part... for the custom post type. Try this and see if it works.