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

Liam Maclachlan
Liam Maclachlan
22,805 Points

How do you correctly display custom post types in wordpress loop?

So, this is a bit more specific than the question is portraying.

My custom query is for a custom post type (Using the plugin CPT UI), I have activated thumbnail support in the functions.php and have created the category "vendor" (slug = "vendor").

The loop I am using is this:

                    <ul class="carousel-inner" role="listbox">

                        <?php $args = array('category_name' => 'vendor'); ?>
                        <?php $loop = new WP_Query($args); ?>
                        <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <li class="item">
                            <h1>There is a post!</h1>
                        </li>

                        <?php endwhile; ?>

                        <?php else: ?>
                            <h1>No posts here!</h1>
                        <?php endif; ?>
                        <?php wp_reset_postdata(); ?>

                    </ul>

When I run the query no results are returned... any ideas?

1 Answer

Liam Maclachlan
Liam Maclachlan
22,805 Points

Fixed! Huge div moment. Insead of waisting my time searching by category, I search by 'post_type=vendor' instead... funnily enough this works!! Code below.

                    <ul class="carousel-inner" role="listbox">

                        <?php $args = array('post_type' => 'Vendor'); ?>
                        <?php $loop = new WP_Query($args); ?>
                        <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

                        <li class="item">
                            <h1>There is a post!</h1>
                        </li>

                        <?php endwhile; ?>

                        <?php else: ?>
                            <h1>No posts here!</h1>
                        <?php endif; ?>
                        <?php wp_reset_postdata(); ?>

                    </ul>