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

Wordpress theme - adding content in custom places part I

Hi guys, I am trying hard with this tutorial and my code exactly matches that used by Zac in the video (I think), but I cannot get my testimonial to appear underneath my 'about' section or on any of the other pages. I press refresh but it does not appear. My guess is, there is a step that I am missing out earlier on in the video series where I was supposed to add a testimonial via the custom post types in the 'Work' section? Despite that, it is there now! Can anyone shed some light on this issue? Regards, Robert Young, London UK <div class="divider grid_12"> <h5>Testimonial</h5> </div>

        <?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; endif; ?>

6 Answers

Kevin Korte
Kevin Korte
28,149 Points

In your custom post type testimonials, do you have a field set up with the field name of testimonial?

Also, you'll want to make sure that you are using the correct page template. While in development, I adopted Zac's idea of putting some sort of line that tells what template file I'm on so that I can confirm WP is using the template I expect it to be using, and not another template file down in the hierarchy.

For instance, if you have a custom.php file, and you think it's using that file, but in reality it's using the single.php file. That is no bueno, so you'll want to check that too.

See below.

Hi Kevin,

Thanks for your reply,

Not sure I completely understand you,

In my custom post types in my 'Work' section, when I select 'Shirts-4-Mike,' I see all the post types I am using - at the bottom I have one called 'Related Testimonials', where it allows me to select any related testimonials from a list (which has all my pages). Currently I have selected 'Mike the Frog' from the testimonials section which I added recently. Is this set up correctly?

Also, how do I check which page template it is that I am using? (e.g. for the 'About' page).

Regards,

Robert :)

Kevin Korte
Kevin Korte
28,149 Points

I'm a bit confused myself now. Let's take a step back.

  1. You've downloaded the Custom Post Types UI plugin for Wordpress
  2. You've created a new custom post type called "testimonials"
  3. You've downloaded the Advance Custom Fields plugin for Wordpress
  4. You created new fields in that plugin. One being called "testimonial", which is probably a textarea, and one called "name" which is probably just a single line text input
  5. You set this new custom field group to show when "post type is equal to testimonials"
  6. You should now have a new left hand tab like your post and page tabs called "testimonals"
  7. You'll want to create a new "testimonial".
  8. You should see the default title and content box, and also you should see your two new fields, "testimonial" and "name"
  9. You need to put the testimonial and name in their respective box, and click publish.
  10. Repeat steps 7-9 for each testimonial

To check your page template, you have to know what template you are working on. Is this single.php, page.php or some custom name like work.php? Usually, below the header I just put a line that says

<p>This is the single.php file</p>

or whatever the file name is. That way I know what file wordpress is using. Does that help you? I couldn't quite follow what you doing in your last post. Maybe screenshots might help clear up some of the confusion.

Hi Kevin - just checked I have already completed steps 1-10. I have used the technique you have mentioned, and the template that I am working on for the About page is 'page.php'. Should it be using this template? I have two testimonials I the section now, 'Mike the Frog' & 'Dr. Wattz', and neither seem to be appearing on the About page...

Kevin Korte
Kevin Korte
28,149 Points

Okay, so a couple more questions.

When you view your page source for this page, do you at least see your empty HTML that is inside of the loop?

Is this loop in the file page.php?

You will want to take a look at this page http://codex.wordpress.org/Template_Hierarchy and understand the template hierarchy wordpress uses. The fact that WP is using page.php for the about page is not necessarily the problem, unless this loop is not in the page.php file. Just understand, page.php is uses as a default templage for all pages, and likely for something like an about page, you will want to structure of that page to be different than other pages on the site, in most cases.

Let me know on those two questions and we'll move on from there. As you can see, there are a lot of moving parts here, which means a lot of fail points.

Good Afternoon Kevin (or Good Morning in Florida),

  1. Yes. I see the 'Lorem ipsum dolor sit amet' text in the source, which is the content for the about page, and the title <h3>About</h3>, both of which appear inside of the loop,

  2. The loop is in the file of page.php.

Now that is ok, how do we incorporate the testimonial into this page? (I have managed to go on in the tutorial and add widgets etc.).

Robert

Ah, I have just seen that my 'page.php' file is missing a crucial line:

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

So, now the testimonials are appearing on the about page :)

Although they are surrounded by the text,   !

Hi Kevin, It seems to work now. Consider this problem resolved. Thanks a lot for your help! :)

Kevin Korte
Kevin Korte
28,149 Points

Glad to hear it! Your solution made total sense.