Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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 threadthis 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.

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.

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.

replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

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 threadthis 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.

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.

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.

Source Link
nickpish
  • 217
  • 5
  • 16

Append taxonomy terms as class names in markup?

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.