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 Build a Website with WordPress Custom Post Types and Fields in WordPress Custom Post Type Templates

Jagoda Jovanovic
Jagoda Jovanovic
1,420 Points

The new pages don`t show in the project download folder.

Hello, in the video about Custom Post Types Templates, there are 3 new pages (related to art custom fields) imported to the child theme from the parent theme(I suppose that is the project download folder?). I have been following the tutorial and made the custom fields named gallery-galleries and I can see them on my dashboard, nevertheless they don`t show up anywhere else. Could you tell me how can fix this and where I might made a mistake? Also, I was wondering if these Custom Post Type pages could be used for galleries as well, with let's say 20 photos per page?

Thank you in advance, I am very new to WordPress and I am still a little bit lost.

2 Answers

Adding custom post types manually or using a plugin only adds the functionality of adding and editing custom posts in the backend (WordPress Dashboard). The data is stored in your database. To output this data on the frontend, you need custom template files. Compare the code in the tutorial with your code and check for any differences.

Also, I was wondering if these Custom Post Type pages could be used for galleries as well, with let's say 20 photos per page?

Jup, you can loop through custom post types just like default posts. You'll need to create a custom page template and add a custom loop to achieve this. Example Code for looping through 20 custom posts in a custom page template below. Just change the name of the custom post type to the name you defined. In my example code bellow i created a variable called $custom_post_type and gave it the string value of 'example'. Replace 'example' with the name of your custom post type.

Inside of the while loop add the code that displays the data of your custom post type you want to output in the frontend. e.g. the_title(); the_content(); the_post_thumbnail(); etc.

If you are using custom fields with the "Advanced Custom Fields" plugin you can add field functions here too. e.g. the_field('fieldname');

 <?php get_header();

/* Template Name: Your Template Name */

 ?>


 <?php 

    $custom_post_type = 'example';
    $post_amount = 20;

    $args = array( 'post_type' => $custom_post_type, 'posts_per_page' => $post_amount);

    $loop = new WP_Query($args);

     if ($loop->have_posts()):

            while ($loop->have_posts()) : $loop->the_post(); ?>

                <!-- Post content functions comes here  e.g. the_title(); -->

            <?php endwhile;

        endif;

        wp_reset_query();?>


 <?php get_footer();?>

Now to get this all working together you have to create a new page in the WordPress dashboard. Then edit the page and there should be a menu dropdown in the sidebar for choosing a template. Choose the template name you defined in your code. (in the example above this would be: "Your Template Name")

Jagoda Jovanovic
Jagoda Jovanovic
1,420 Points

@Selloween, thanks a lot for your response!