get_the_tags Conditional display of a custom taxonomy

I’m currently using the below code for displaying standard post tags in a sidebar, given the current post has one associated with it. If not, no tag & accompanying headline, etc., should be displayed. Also, the usual /tags slug should be removed in the URLs, respectively. Again, my below code does that, which is exactly what I want.

However, how can I accomplish the same thing with a custom taxonomy of, say, “rivers”?

Read More

Your guidance is much appreciated as I’ve tried several things already, to no avail.

The current code (in use for standard post tags):

<?php
    $tags = get_the_tags();
    if ($tags) {
        echo '<h2>Holiday Regions</h2>
        <p class="rightP">';
            foreach ($tags as $tag) {
                echo '<a href="' . get_option('siteurl') . '/' . $tag->slug . '" title="' . sprintf( __( "Learn more about the %s" ), $tag->name ) . '"' . '>' . $tag->name.'</a><br />
        ';
                }
        echo '</p>';
        }
    ?>

For those who are in a similar situation, here’s what did the trick—thanks to the hint from @Michael:

<?php
    $terms = get_the_terms( $post->ID, 'rivers' );
    if ($terms) {
        echo '<h2>Rivers</h2>
        <p class="rightP">';
            foreach ($terms as $term) {
                echo '<a href="' . get_option('siteurl') . '/' . $term->slug . '" title="' . sprintf( __( "Learn more about the %s" ), $term->name ) . '"' . '>' . $term->name.'</a><br />
        ';
                }
        echo '</p>';
        }
    ?>

Related posts

Leave a Reply

1 comment