I have created a page template to display specific data. Is it a bad idea to copy the loop from page.php and paste it as is in the template file? Does this mess with the loop? Doing this has made my page just as I need it. I tried a custom query using WP_Query but that didn't want to work

I am using a custom query in a page template but it displays nothing. What am I missing?

 $query = new WP_Query();

 if( $query->have_posts()): while($query->have_posts()): $query->the_post();

    get_template_part( 'template-parts/page/content', 'page' );

    // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :

   endwhile; // End of the loop.
  • If you want to loop over the same posts twice, sure, but I'm not sure what else you'd need to do that for. – Jacob Peattie Jan 12 '18 at 4:11
  • 1
    How did you try the custom WP Query, and why it didn't work? – D. Dan Jan 12 '18 at 7:20

It seems all I needed to do was put in the template_part

 get_template_part( 'template-parts/page/content', 'page' );

I think it's okay to do that. problem will arise if you don't name the file in proper way. i.e., wordpress will try to load that file according to template hierarchy (in case you want to explore what wp template hierarchy is visit: https://wphierarchy.com/) If you are trying to make a custom post type then rename the file according to your post type slug(e.g. if your custom post type is testimonials then name the file page-testimonials.php). I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.