2

I've defined a custom post type for "person", and I'd like to append class names to the output markup based on taxonomies that correspond to team names. For example, if a person is part of the "creative" and "interactive" teams, I'd like to output the following:

<div class="person creative interactive">Jane</div>

I found this thread on the forum, but in my case I need to output the taxonomy names within an echo statement, as the output is defined as a function in my functions.php file.

Here is a reduced version of what I've come up with so far, based on the aforementioned thread and this Stack Overflow thread:

function display_all_people() {
    // set up arguments for new post query
    $args = array(
        'post_type' => 'people',
        'order' => 'ASC',
        'orderby'    => 'meta_value',
        'meta_key'   => 'last_name'
        );

    $peoplePosts = new WP_Query( $args );
    if ( $peoplePosts->have_posts() ) {
        echo '<div class="people-listing">';
        while ( $peoplePosts->have_posts() ) {
            $peoplePosts->the_post();
            $terms = get_the_terms( $post->ID, 'teams' );

            foreach ($terms as $term) {
                echo '<div class="person' . implode('', $term->slug) . '">';
            }

            echo '<div>More markup for each person listing</div>';
            echo '</div>';
        } 
        echo '</div>';
    } else {
        return '<p>Nothing Here.</p>';
    }
    wp_reset_postdata();
}

Thus, I'm trying to use the implode() to concatenate the values of the array (i.e. "team" taxonomy names), but it doesn't seem to be working (PHP is throwing errors.) Any idea how I can successfully append the taxonomies as class names in this way? Thanks for any assistance here.

  • Create an array of values first, and echo the implode after. – jgraup Nov 21 '16 at 1:16
3

Differ the output until you have a collection ready.

$terms = get_the_terms( $post->ID, 'teams' );

// create a collection with your default item
$classes = array('person');

foreach ($terms as $term) {

    // add items
    $classes[] = $term->slug;

}

// output all the classes together with a space to separate them.
echo '<div class="' . implode(' ', $classes) . '">';

echo '<div>More markup for each person listing</div>';

EXCLUDING

To ignore adding any item:

if( $term->slug !== 'accounting' ) { $classes[] = $term->slug; }

To remove all bad apples:

$pros = array('A', 'B', 'C');
$cons = array('C');
$best = array_diff($pros, $cons);
print_r ($best); // A, B

In context:

$terms = get_the_terms( $post->ID, 'teams' );

// create a collection with your default item
$classes = array( 'person' );

if ( $terms && ! is_wp_error( $terms ) ) {
    foreach ( $terms as $term ) {
        $classes[] = $term->slug; // add items
    }
}

// remove any specific classes here
$classes = array_diff( $classes, array( 'creative-services' ) );

// output all the classes together with a space to separate them.
echo '<div class="' . implode( ' ', $classes ) . '">';

echo '<div>More markup for each person listing</div>';
  • so I just tried your approach, and it seems to be working as far as adding the class name defined as a default-- i.e. "person"-- but it's not adding those defined by the custom taxonomy. I'm wondering if there's something wrong with my the way I'm trying to collect the terms, i.e. $terms = get_the_terms( $post->ID, 'teams' );? – nickpish Nov 21 '16 at 2:56
  • I actually just figured it out-- first off, I was referencing the taxonomy name incorrectly-- it's "team" not "teams"-- and I need to wrap the foreach loop in an if statement in case no taxonomy terms have been defined for a given post. Now it's all working-- thanks again. – nickpish Nov 21 '16 at 3:28
  • one other quick question on this topic-- can I easily exclude a specific taxonomic group, let's say "accounting" within "teams" (and show all other groups) in creating the array as above? – nickpish Nov 22 '16 at 0:59
  • 1
    if( $term->slug !== 'accounting' ) $classes[] = $term->slug; – jgraup Nov 22 '16 at 1:00
  • Added an array_diff() example above as well. – jgraup Nov 22 '16 at 1:05

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.