• I am trying to create a single-{post_type}.php template file, I don’t know why my custom post type template is returning all the posts. Not sure where i’m going wrong, can anyone help me out?

    <?php
    
    $args = array ( 'post_type' => 'foxware_recipe' );
    
     $the_query = new WP_Query ( $args );
    
    ?>
    
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    	<h3><?php the_title(); ?></h3>
    	<?php the_content(); ?>
    	<hr>
    
    <?php endwhile; else: ?>
    
    	<p>There are no posts or pages</p>
    
    <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Julian Fox (greataussiepie)

    (@greataussiepie)

    change this line `<?php
    $args = array( ‘post_type’ => ‘foxware_recipe’ );

    ?>`

    to

    <?php
    $args = array( 'post_type' => array( 'foxware_recipe' ), 'posts_per_page' => '1' );
    ...
    ?>

    seemed to do the trick. Although not sure if that is the correct standard.

    Check the codex on WP_Query.

    You’ll want to add a posts_per_page limit to your $args.

    $args = array('post_type' => 'foxware_recipe', 'posts_per_page' => 1);
    Thread Starter Julian Fox (greataussiepie)

    (@greataussiepie)

    thanks for confirming that ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘return a single post in a loop’ is closed to new replies.