0

I want to keep this category title on my pages, but I don't want it to be a link.

<?php echo get_the_term_list( get_the_ID(), 'portfolio_cats', ' ', ' , ', ' '); ?>

Can anyone help? Thanks

2
3

You could use get_the_terms() and wp_sprintf_l():

function wpse_52878_term_list( $args = array() )
{
    $default = array (
        'id'               => get_the_ID(),
        'taxonomy'         => 'post_tag',
        'before'           => '',
        'after'            => '',
    );

    $options = array_merge( $default, $args );
    $terms   = get_the_terms( $options['id'], $options['taxonomy'] );
    $list    = array();

    foreach ( $terms as $term )
    {
        $list[] = $term->name;
    }
    return $options['before'] . wp_sprintf_l( '%l', $list ) . $options['after'];
}

echo wpse_52878_term_list( array ( 'id' => get_the_ID(), 'taxonomy' => 'portfolio_cats' ) );

Another option:

echo wp_strip_all_tags( 
    get_the_term_list( get_the_ID(), 'portfolio_cats', ' ', ' , ', ' ') 
);
1
  • Thank you so much. You just saved me a whole lot of time and stress! – Toronto May 22 '12 at 14:22
0

Why not use this?

<?php
$portfolio_cats = wp_get_object_terms(get_the_ID(), 'portfolio_cats', array('fields' => 'names'));
if(!empty($portfolio_cats)){
    if(!is_wp_error( $portfolio_cats )){
        echo '<ul>';
        foreach($portfolio_cats as $term){
            echo '<li>'.$term->name'</li>'; 
        }
        echo '</ul>';
    }
}

Try a print_r with the returned array to see the contents.

3
  • @joshua The Code is being used to display the category as the page header. However I don't want it to link back to a page containing all of the category items. currently it displays like this: <h1><a href="../portfolio-category/branding/"rel="tag"> Branding</a></h1> – Toronto May 22 '12 at 5:41
  • @joshua Sorry I'm not sure how to put the code in a box. <?php echo get_the_term_list( get_the_ID(), 'portfolio_cats', ' ', ' , ', ' ') ?> – Toronto May 22 '12 at 5:42
  • You should use get_the_terms instead of wp_get_object_terms. wp_get_object_terms bypasses WordPress' caching, and essentially means you query the database with every call. – Stephen Harris May 22 '12 at 12:10

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.