• Hi all,
    I have been struggling with this all day. I have custom taxonomy info for my posts displayed in my posts using

    <?php
    the_terms( $post->ID, 'taxonomy_name', '<dt>Taxonomy Name:</dt> ', ', ', '' );
    ?>

    However, the taxonomy info is always shown as a link.
    For example…
    Taxonomy Name: output

    How do I stop this, only showing the “output” part as plain text, and still keep the dt tags and “Taxonomy Name”? I found one other topic on this, and only two of the replied suggestions came close to working. One stripped the dt tags, the other did not display “Taxonomy Name”. Please help.

Viewing 12 replies - 1 through 12 (of 12 total)
  • There’s probably a better way, but since we’re in the ‘Hacks’ Forum, try this hack out:

    $terms = get_the_terms( $post->ID, 'taxonomy_name', array( 'fields' => 'names' ) ) ;

    You’ll have to check the output of get_the_terms — it is an array, but I’m not sure if that’ll produce a list of links, or a list of objects with ‘name’ properties set ( you know $term[0]->name ). Sorry, I don’t have the ability to test this at the moment.

    If it produces a list of links in your foreach loop, call strip_tags on the item to remove the a tag.

    Hope this at least points you in the right direction.

    Minor error:

    The format of get_the_terms follows the same format as get_terms, with an additional parameter for the post id, so the call above should be:

    $terms = get_the_terms( $post->ID, 'tagonomy_name', "fields=names") ;

    check the codex page for get_terms for available parameters to get_the_terms: http://codex.wordpress.org/Function_Reference/get_terms

    Thread Starter Seijun

    (@seijun)

    I put

    <?php
    $terms = get_the_terms( $post->ID, 'operating_system', "Operating System") ;
    ?>

    into my single.php file.

    This results in no taxonomy info of any kind being shown.

    Thread Starter Seijun

    (@seijun)

    Ok, I figured out one way!
    Earlier I mentioned that one of my attempts worked except for stripping out ALL formatting. This was using strip_tags, and I remembered that I could tell strip_tags to allow certain tags. This worked:

    <?php
    $terms_as_text = get_the_term_list( $post->ID, 'operating_system', '<dt>Operating System(s):&nbsp</dt>', ', ', '' ) ;
    echo strip_tags($terms_as_text, '<dt>,&nbsp');
    ?>

    If anyone has a better idea, please let me know.

    Also, what does the extra , ‘, ‘, ” at the end of my string mean? This was code taken from the wp codex and on the forum here.

    If what you are doing works for you, good deal. As for the code you posted, you edited the $args parameter incorrectly:


    <?php
    $terms = get_the_terms( $post->ID, 'operating_system', "fields=names") ;
    ?>

    The "fields=names" is required to cause the get_the_terms method to return only the names. The details of how this works are here: http://codex.wordpress.org/Function_Reference/get_terms

    Also, what does the extra , ‘, ‘, ” at the end of my string mean? This was code taken from the wp codex and on the forum here.

    Usage
    <?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ; ?>

    http://codex.wordpress.org/Function_Reference/get_the_term_list

    Thread Starter Seijun

    (@seijun)

    @ aut0poietic:: Thank you for taking the time to reply. I tried the code in your last post

    $terms = get_the_terms( $post->ID, 'operating_system', "fields=names") ;

    But the results were same as before. No output of any kind, as if the code did not even exist.

    autopoietic was correctly referring to get_terms in the link he gave but wrote get_the_terms in his code. Probably because of inconsistent naming conventions in WordPress because in some places it’s the_something() while other functions are just something(). πŸ˜€

    Thread Starter Seijun

    (@seijun)

    Putting get_terms instead of get_the_terms also has no effect.

    To clarify differences:

    get_terms()
    – is for getting all(or selected) terms from a taxonomy.

    get_the_terms()
    – is for getting the terms of a taxonomy associated with a given post.

    Sorry my post was confusing — that’s what I get for trying to do too many things at once, with no way to test code πŸ˜‰

    I’ll stick with what I’ve said: If what you’re doing works, stick with it. No reason to waste time you could be playing Reach ;-D

    However, if you want to see what I was trying to explain, here’s some working code and explanation.

    <?php $terms = get_the_terms( $post->ID, 'technology' ) ; ?>

    get_the_terms returns an array of objects that look a something like the output below internally:

    Array
    (
        [21] => stdClass Object
            (
                [term_id] => 21
                [name] => windows xp
                [slug] => windows-xp
                [term_group] => 0
                [term_taxonomy_id] => 21
                [taxonomy] => operating_system
                [description] =>
                [parent] => 0
                [count] => 1
                [object_id] => 116
            )
    ...
    )

    That’s the difference between the_terms and get_the_terms “get” returns the data instead of echoing. This is a pretty common thing in WP, adding “get_” to a function returns what you were after, rather than echoing it — but not always; I wish it were universal.

    Anyway, so you’ll have to do the work on this one and print out what you need:

    <dt>Taxonomy Name:</dt>
    <?php
    	$terms = get_the_terms( $post->ID, 'technology' ) ;
    	$term_list = array_pop($terms)->name ;
    	foreach( $terms as $os )
    	{
    		$term_list .= ", " . $os->name ;
    	}
    	echo $term_list ;
    ?>

    That (tested on 3.01, finally) should do what you want and give you the flexibility to output whatever you want. But as I said, if the code you had above does what you want, there’s no real reason to redo it.

    @aut0poietic – thank you for that. I was looking to do the same thing, and that saved me a lot of headaches. Back to coding!

    For anyone reading this thread, currently at the top of Google, there’s another thread here which I found helpful:

    http://wordpress.org/support/topic/trying-to-output-a-posts-terms-taxonomy-as-text-not-urls

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom taxonomies: getingt rid of link on display??’ is closed to new replies.