0

I'm trying to take a list of tags and list them on the page but without linking to their archive page.

At the moment I'm doing this:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_the_term_list( $post->ID, 'visits', 'Visits ', ', ', ' ' );
wp_reset_query();
?>

Thanks

3 Answers 3

4

You can use get_the_terms(). I've adapted the following from an example on that page:

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

if ( $terms && ! is_wp_error( $terms ) ) : 

    $visits_name = array();

    foreach ( $terms as $term ) {
        $visits_name[] = $term->name;
    }
    $terms_list = join( ", ", $visits_name );
    echo $terms_list;
endif;

EDIT:

Using wp_list_pluck, as suggested by Telos, is much easier:

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

if ( $terms && ! is_wp_error( $terms ) ) : 
  echo join( ',', wp_list_pluck( $terms, 'name' ) );
endif;
2
  • 1
    You could consider using wp_list_pluck to do the name plucking for you. Oh, and your if condition is not closed.
    – t31os
    Commented May 3, 2013 at 9:56
  • Yes, lame copy/paste from the codex. Thanks for the wp_list_pluck!
    – RRikesh
    Commented May 3, 2013 at 10:14
0

List all the terms in a custom taxonomy, without a link:

$terms = get_terms("my_taxonomy");
 $count = count($terms);
if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
      echo "<li>" . $term->name . "</li>";
    }
     echo "</ul>";
 }  

List all the terms, with link to term archive, separated by an interpunct (·). (language specific, WPML method):

$args = array( 'taxonomy' => 'my_term' );
$terms = get_terms('my_term', $args);
$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '<p class="my_term-archive">';
    foreach ($terms as $term) {
        $i++;
        $term_list .= '<a href="/term-base/' . $term->slug . '" title="' .sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
        if ($count != $i)
          $term_list .= ' &middot; '; 
        else 
           $term_list .= '</p>';
    }    
echo $term_list;}

For more details: get_terms().

3
  • Thanks. How can I put a comma between the terms rather than a new line? I'm messing up the <li> Commented May 3, 2013 at 8:15
  • Please follow this URL : codex.wordpress.org/Function_Reference/get_terms if you are not gating this then please let me know. Commented May 3, 2013 at 8:19
  • The user is fetching terms associated with a post ID, get_terms will not be suitable.
    – t31os
    Commented May 3, 2013 at 9:57
0

Well, I'd use this :

echo strip_tags(get_the_term_list( $post->ID, 'visits', 'Visits ', ', ', ' ' ));

EDIT : We use strip_tags() to deactivate link. This has to be used within the loop.

2
  • "This answer was automatically flagged as low-quality because of its length and content", which I am sure you don't want. Please try to make your answers more substantial. Add an explanation of your solution, links to the codex or other sources...
    – s_ha_dum
    Commented May 3, 2013 at 14:15
  • Am I clear enough?
    – JMau
    Commented May 4, 2013 at 12:17

Your Answer

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

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