Last active
September 4, 2018 11:14
-
-
Save jazzsequence/5415007 to your computer and use it in GitHub Desktop.
Get taxonomy links in WordPress for a custom post type and custom taxonomy, similar to get_the_category_list() or get_the_term_list() but is a bit smarter about separators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if ( 'post_type' == get_post_type() ) : | |
$term_list = get_the_terms( $post->ID, 'custom_taxonomy' ); // get the taxonomy terms | |
if ( count($term_list) > 2 ) { // if there are more than 2 terms, use a comma, e.g.: this, this, that | |
$sep = __( ', ', 'textdomain' ); | |
} elseif ( count($term_list) === 2 ) { // if there are EXACTLY 2 terms, use an "and", e.g.: this and that | |
$sep = __( ' and ', 'textdomain' ); | |
} else { // there there is only one (or no) terms, don't use anything | |
$sep = null; | |
} | |
$sep_num = 1; // we'll use this as a counter later | |
?> | |
<span class="cat-links"> | |
<?php | |
_e( 'Filed in ', 'textdomain' ); | |
foreach ( $term_list as $term ) { | |
echo '<a href="' . get_term_link( $term->slug, 'custom_taxonomy' ) . '">' . $term->name . '</a>'; // echo the term | |
if ( $sep_num < count($term_list) ) { // if the counter is less than the number of terms, use a seperator | |
if ( count($term_list) - $sep_num == 1 ) { // if the difference between the two is down to 1, this is the last term, so use an "and", e.g.: this, this and that | |
_e( ' and ', 'textdomain' ); | |
$sep_num++; | |
} else { // otherwise, keep using the default separator | |
echo $sep; | |
$sep_num++; | |
} | |
} | |
} | |
?> | |
</span> | |
<?php endif; // End if 'post_type' == get_post_type() ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you please show the template of the page where
<a href="' . get_term_link( $term->slug, 'custom_taxonomy' ) . '"> </a>
will redirect me ???Because clicking the permalink says: "No page found".
Even though I have used: taxonomy-{taxonomy}-{term}.php format to create specific template for that taxonomy and term