admin管理员组

文章数量:1122846

I've searched far and wide to try and find an answer to my question. I'm hoping I can get help here. Here goes...

I'm currently retrieving the terms of my custom taxonomy using:

<?php echo get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ); ?>

What I'm trying to do is retrieve these same post-specific custom taxonomy terms in a list without them being output as links. I've tried all of the following "solutions," but none of them work. Any help would be appreciated.

Returns the post-specific terms in one long string that can't be put in a list:

$terms_as_text = get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ) ;
echo strip_tags($terms_as_text);

Returns a list of all the terms used across all the custom post types:

<ul>
<?php $args = array( 'taxonomy' => 'skills', 'orderby' => 'ID', 'order' => 'ASC' );
$categories = get_categories( $args );
foreach($categories as $category) { echo '<li> '. $category->name . '</li>'; } 
                ?>
</ul>

Returns nothing:

<?php $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
wp_get_object_terms( $post->ID, $skills, $args );
?>

I've even tried get_the_terms, get_terms, and get_categories to no avail.

I've searched far and wide to try and find an answer to my question. I'm hoping I can get help here. Here goes...

I'm currently retrieving the terms of my custom taxonomy using:

<?php echo get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ); ?>

What I'm trying to do is retrieve these same post-specific custom taxonomy terms in a list without them being output as links. I've tried all of the following "solutions," but none of them work. Any help would be appreciated.

Returns the post-specific terms in one long string that can't be put in a list:

$terms_as_text = get_the_term_list( $post->ID, 'skills', '<ul><li>', '</li><li>', '</li></ul>' ) ;
echo strip_tags($terms_as_text);

Returns a list of all the terms used across all the custom post types:

<ul>
<?php $args = array( 'taxonomy' => 'skills', 'orderby' => 'ID', 'order' => 'ASC' );
$categories = get_categories( $args );
foreach($categories as $category) { echo '<li> '. $category->name . '</li>'; } 
                ?>
</ul>

Returns nothing:

<?php $args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
wp_get_object_terms( $post->ID, $skills, $args );
?>

I've even tried get_the_terms, get_terms, and get_categories to no avail.

Share Improve this question edited Jun 4, 2024 at 20:16 Jesse Nickles 7357 silver badges19 bronze badges asked Jul 21, 2011 at 0:20 Certified KCertified K 531 gold badge1 silver badge3 bronze badges 1
  • 1 Related: wordpress.stackexchange.com/questions/8284/… – Jesse Nickles Commented Jun 4, 2024 at 8:41
Add a comment  | 

8 Answers 8

Reset to default 11

Can try this:

$terms = get_the_terms ($post->id, 'skills');
if ( !is_wp_error($terms)) : ?>

<?php 
    $skills_links = wp_list_pluck($terms, 'name'); 

    $skills_yo = implode(", ", $skills_links);
    ?>

    <span><?php echo $skills_yo; ?></span>

Just use strip_tags

<?php echo strip_tags(get_the_term_list( $post->ID, 'CUSTOM-TAXONOMY', ' ',', ')); ?>
$terms = wp_get_post_terms($post->ID, 'TAXONOMYNAME');
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
function term_clean($postid, $term)
{
    $terms = get_the_terms($postid, $term); 
    foreach ($terms as $term) {  echo $term->name;   };

}

If you just want the terms assigned to a specific post:

<?php $object_terms = wp_get_object_terms( $post_id, 'skills', array( 'fields' => 'names' ) );
if ( $object_terms ) { ?><ul><li><?php echo implode( '</li><li>', $object_terms ); ?></li></ul><?php } ?>

If you want ALL of the terms:

<?php $all_terms = get_terms( 'skills', array( 'fields' => 'names' ) );
if ( $all_terms ) { ?><ul><li><?php echo implode( '</li><li>', $all_terms ); ?></li></ul><?php } ?>

I ran into a similar problem yesterday, and came up with the follow solution:

function taxonomy_list( $taxonomy ) {
    $args = array('order'=>'ASC','hide_empty'=>false);
    $terms = get_terms( $taxonomy, $args );
    if ( $terms ) {
        printf( '<ul name="%s">', esc_attr( $taxonomy ) );
        foreach ( $terms as $term ) {
            printf( '<li>%s</li>', esc_html( $term->name ) );
        }
        print( '</ul>' );
    }
}

Then, just paste <?php taxonomy_list( 'TAXONOMY ID' ); ?> in your template file, replacing TAXONOMY ID with whatever the name of the taxonomy is.

My original usage was to create a list of the job categories I have on my job board. Each one then linked to the taxonomy's archive. You can see the full function in my answer on my own Stackoverflow question.

// to display taxonomy terms without links: separated with commas
// copy this code in your function.php

function get_taxonony_toDisplay($post_id, $taxonomy_name) {
$terms = wp_get_post_terms($post_id, $taxonomy_name);
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        echo $term->name . ", ";
    }
}
}

Since I had to display 3 taxonomies separated with commas, so I made a function using Henry's code.

To display use the following line:

<?php get_taxonony_toDisplay($post->ID, 'your_taxonomy_name' ); ?> 

If you want the terms ordered by slug rather than name then use this...

<?php if(has_term('', 'CUSTOM-TAX')) {?> 
<?php
    $custom_terms = get_the_terms( get_the_ID(), 'CUSTOM-TAX' );
    // Make sure we have terms and also check for WP_Error object
    if (    $product_terms
    && !is_wp_error( $product_terms )
    ) {
    @usort( $product_terms, function ( $a, $b )
    {
    return strcasecmp( 
    $a->slug,
    $b->slug
    );
    });
    // Display your terms as normal
    $term_list = [];
    foreach ( $custom_terms as $term ) 
    //$term_list[] = esc_html( $term->name );  // comment this line out if you want the terms linked  and visa versa
    $term_list[] = '<a href="' . get_term_link( $term ) . '">' . esc_html( $term->name ) . '</a>'; // comment this line out if you DON'T want the terms linked and visa versa
    echo implode( ', ', $term_list );
    }
?>                          
<?php } else { ?><?php }?>

本文标签: How to list custom taxonomy terms without the hyperlinks