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 Custom Post Types

Hi everybody,

I've built a HTML version of this site -

http://nomaankhalid.edeharrison.com/timetables-memberships.html

and I'm in the process of transferring it over to WordPress.

I'm using the plugins Advanced Custom Fields and Custom Post Types UI, but I'm a bit lost as to how to build the timetable area.

So far, I've created a post type ("class"), custom fields ("time" and "class_name") for each post (each post will be a new gym class), and categories for the days of the week ("Monday", "Tuesday", "Wednesday", etc).

In my PHP I want to be able to get each post by their category name (the day of the gym class), and list the classes of each day in a table.

So far I have (for Monday):

<table>
    <tr class="title">
      <td colspan="2">Monday</td>
    </tr>
    <?php
    $args = array (
        'post_type' => 'class',
        'category_name' => 'monday'
    );
    $query = new WP_query( $args );
    ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <tr>
        <th><?php the_field('time'); ?></th>
        <td><?php the_field('class_name'); ?></td>
    </tr>
    <?php endwhile; endif; ?>
</table>

The posts aren't displaying though.

I know I probably shouldn't be building a new table for each day, but I would be happy if I just got this working!

Any guidance/ideas would be much appreciated.

Cheers,

Ede

2 Answers

Okay.. You've to add $query-> before...

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

Ah, of course. That worked perfectly.

Just to clarify in case anyone else might benefit, the finished code is (for Monday):

<!--Monday-->
<table>
    <tr class="title">
        <td colspan="2">Monday</td>
    </tr>
    <?php
    $args = array (
        'post_type' => 'class',
        'category_name' => 'monday'
    );
    $query = new WP_query( $args );
    ?>
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
    <tr>
        <th><?php the_field('time'); ?></th>
        <td><?php the_field('class_name'); ?></td>
    </tr>
    <?php endwhile; endif; ?>
</table>

Cheers for your help Golam,

Ede

You said you used post type ("post"), again on the code I see you use 'post_type' => 'class'. what You've created with the plugin actually? You shouldn't create post type "post" as its using WordPress already.

Thanks for the reply Golam. I just made a mistake in my explanation. I meant post type ("class"). I'll edit that now.

Cheers,

Ede