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

How do I use this method to display the recent posts from my custom post type? Like the portfolio posts in your example.

Wordpress Theme Development

1 Answer

Vince Brown
Vince Brown
16,249 Points

Hey David, I don't know exactly what method you are referring to in the lesson but it sounds like you want to run a loop over a custom post type. To achieve this we are going to make a new instance of WP_Query.

<?php


  // These are the arguments for your new wp query you are creating
  $loop_args = array (
    'post_type' => 'your-post-type',
    'posts_per_page' => 6,    // -1 would show all posts 
  );


  $custom_loop = new WP_Query( $loop_args );


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



     <p> This is where you do your markup you want displayed for each post </p>
     <?php the_title( '<h1 class = "post__title">', '</h1>' );



  <?php endwhile; wp_reset_query(); ?>



 <?php // Do not forget to reset query after you are done with loop!

Check out the Codex as well https://codex.wordpress.org/Class_Reference/WP_Query there is a lot of ways to filter the query by cats, taxonomies etc...

Hope this helps!

Hi Vince, thanks for your answer. Sorry that i didn't quite explain myself properly. What I meant was, how can I display the recent posts from my custom post type in the sidebar using widgets? Is it even possible without a plugin? Thanks.