I want to build a page with the members of a team. Because I have too many members, I want to group them by department. Therefore, I created a custom post type TEAM with taxonomy by department

I use the following code to display the team members. I created a new page template.

$team_posts = get_posts( array(
    'post_type' => 'team',
    'posts_per_page' => -1, // Unlimited posts
    'orderby' => 'title', // Order alphabetically by name
) );

    foreach ( $team_posts as $post ): 

    $thumb_src = null;
    if ( has_post_thumbnail($post->ID) ) {
        $src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'team-thumb' );
        $thumb_src = $src[0];
    <article class="seven columns team_lid">
        <div class="tm_header">
            <?php if ( $thumb_src ): ?>
            <img src="<?php echo $thumb_src; ?>" alt="<?php the_title(); ?>, <?php the_field('team_position'); ?>" class="img-circle">
            <?php endif; ?>

        <div class="tm-content">
            <h3><?php the_title(); ?></h3>
            <p class="lead position"><?php the_field('team_position'); ?></p>
            <i class="fa fa-mobile contact_icoon"></i><?php the_field('team_phone'); ?><br/>
            <i class="fa fa-mobile contact_icoon"></i><?php the_field('mobile_phone'); ?><br/>

            <i class="fa fa-envelope contact_icoon"></i><a href="<?php the_field('team_email'); ?>"><?php the_field('team_email'); ?></a>
            <div class="content_tekst">
            <?php the_content(); ?>
    </article><!-- END seven columns -->
    <?php endforeach; ?>

This is all working well, but as you can see this code displays all the team members no matter in which department they are.

Can somebody help me with this matter? I want to have the different departments with the team members on 1 page template. Which functions do I have to use.

  • Post the code that sets the $team_posts variable.
    – s_ha_dum
    May 6, 2014 at 11:05
  • @s_ha_dum See my edit. May 6, 2014 at 13:02

1 Answer 1


Follow these steps .

Add the following code to your functions.php

 add_shortcode( 'list-team', 'list_team_members' );
function list_team_members( $atts ) {

    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'post',
        'posts' => 5,
        'category' => '',
    ), $atts ) );

    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'posts_per_page' => $posts,
        'category_name' => $category,
    $query = new WP_Query( $options );
    // run the loop based on the query
    if ( $query->have_posts() ) { ?>
        <ul class="">
            <ul class="">
                <li id="post-<?php the_ID(); ?>">></li>
        $myvariable = ob_get_clean();
        return $myvariable;

and then call any department - [list-team category=" "]

You dont need to create specific template files for this...

shortcodes will do for you..

I wrote this in little hurry...

you can get full code at http://code.tutsplus.com/tutorials/create-a-shortcode-to-list-posts-with-multiple-parameters--wp-32199

  • thanks for the help. I will check it but i rather prefer template files because I want to use a different lay-out on my team page May 6, 2014 at 13:05
  • your wish... you could still create a custom template with different layout and add shortcode for adding team members... May 7, 2014 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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