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

Jeremy Frimond
Jeremy Frimond
14,470 Points

The same custom posts, in two different places, on the same page but with different set of $args: How Too?

Main Content Section: I am making a page that displays employees of a company. the center area displays all the information of the employee.

Nav Section: On the right hand side is an unordered list with the links to all the employees (created in the custom post type).

My end goal is to have only one employee profile displayed in the main content area at a time $args, post per page 1. At the same time I want all the emplyees names to appear in the nav section . Right now I can only get one or the other- everything displayed in both areas or only one display in both areas. What do I need to do to diplay one post in the main area and unlimited posts in the nav area?

Here is my code:

The Args:

<?php 


$args = array(
    'post_type' => 'employee',
        'posts_per_page' => 1
);

$query = new WP_Query( $args );

?>

The loop for the main content area:

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

        <?php get_template_part( 'content' , 'employeeProfile' ); ?>    

    <?php endwhile; endif; wp_reset_postdata();  ?>

The Loop for the employee navigation:

<div class="employeeNav">
      <h1>Our Team</h1>


<ul>

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

        <?php get_template_part( 'content' , 'employees' ); ?>  

    <?php endwhile; endif; wp_reset_postdata(); ?>

</ul>
</div>

2 Answers

Sue Dough
Sue Dough
35,800 Points

-1 in a query shows unlimited post per page.

<?php 


$args = array(
    'post_type' => 'employee',
        'posts_per_page' => 1
);

$query = new WP_Query( $args );

?>

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

        <?php get_template_part( 'content' , 'employeeProfile' ); ?>    

    <?php endwhile; endif; wp_reset_postdata();  ?>


      <h1>Our Team</h1>


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

        <?php get_template_part( 'content' , 'employees' ); ?>  

    <?php endwhile; endif; wp_reset_postdata(); ?>



    <?php 


$args = array(
    'post_type' => 'employee',
        'posts_per_page' => -1
);

$query = new WP_Query( $args );

?>

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

        <?php get_template_part( 'content' , 'employeeProfile' ); ?>    

    <?php endwhile; endif; wp_reset_postdata();  ?>


      <h1>Our Team</h1>


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

        <?php get_template_part( 'content' , 'employees' ); ?>  

    <?php endwhile; endif; wp_reset_postdata(); ?>
Jeremy Frimond
Jeremy Frimond
14,470 Points

Thanks for taking the time to write that out ghost code , brain fart on my end!