Customize term_list output in wordpress custom post type

Sometimes we require some customization for the wordpress “custom post-type” get_the_term_list ‘s output, specifically to meet the theme’s look and usability.

For this discussion we will assume our taxonomy name is “tasks” and we will generate the phrase Projects task(s) before the term_list output.


Process One:

First we will simply customize the look of custom taxonomies output with the “process one” code below –

CODE (Src: WordPress Codex)

<?php echo get_the_term_list( $post->ID, 'tasks', '<b>Project Task(s):</b> ', ' ', '' ); ?>

OUTPUT DEMO
get-the-term-list-output0

But with the above code it becomes a pain (a) when you want to just get rid of the hyperlinks (auto-generated links that the core function insists on using) for the generated set of custom taxonomies in custom post type outputs. Another disadvantage is (b) when you don’t want to show output on the posts where the taxonomy term list is empty. Now comes the below processes to your rescue.


Process Two:

This is a simple php strip_tags command used to customize the look of the output i.e no hyperlink but html tags like bold, italics allowed. You can add this code both parts (PART1& PART2) below in the theme template for the custom post type.

CODE PART1

<?php
function strip_tags_attributes($string,$allowtags=NULL,$allowattributes=NULL){
 $string = strip_tags($string,$allowtags);
 if (!is_null($allowattributes)) {
 if(!is_array($allowattributes))
 $allowattributes = explode(",",$allowattributes);
 if(is_array($allowattributes))
 $allowattributes = implode(")(?<!",$allowattributes);
 if (strlen($allowattributes) > 0)
 $allowattributes = "(?<!".$allowattributes.")";
 $string = preg_replace_callback("/<[^>]*>/i",create_function(
 '$matches',
 'return preg_replace("/ [^ =]*'.$allowattributes.'=(\"[^\"]*\"|\'[^\']*\')/i", "", $matches[0]);'  
 ),$string);
 }
 return $string;
}
?>

CODE PART2

<?php
$string = get_the_term_list( $post->ID, 'tasks', '<b>Project Task(s):</b> ', ', ', '' ) ;
echo strip_tags_attributes($string,'<b><em>','rel');
?><br />

OUTPUT DEMO
get-the-term-list-output1


Process Three:

This process is my personal favorite as it is easy to toggle between both the options i.e hyper-links/ no hyper-links. The code (Part 1) is used as a function so drop this function into ‘functions.php’ and then call it into your template with the next code (Part 2).

CODE PART1 [add to functions.php] (Src: http://codesnipp.it/code/1123)

/**
 * REWRITE OF CORE FUNCTION TO MAKE LINKS OPTIONAL
 * Original function at wp-includes/category-template.php around line 930
 */
function cc_get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $doLinks = 1 ) {
 $terms = get_the_terms( $id, $taxonomy ); 

 if ( is_wp_error( $terms ) )
 return $terms; 

 if ( empty( $terms ) )
 return false; 

 foreach ( $terms as $term ) {
 $link = get_term_link( $term, $taxonomy );
 if ( is_wp_error( $link ) )
 return $link;
 if ($doLinks == 1)    {
 $term_links[] = '<a href="' . $link . '" rel="tag">' . $term->name . '</a>';         
 } else {
 $term_links[] = $term->name;         
 } 

 } 

 $term_links = apply_filters( "term_links-$taxonomy", $term_links ); 

 return $before . join( $sep, $term_links ) . $after;
} 

/**/

Default value for ‘doLinks’ is 1, so you can use it exactly like ‘get_the_term_list’, but if you pass a 0 as the final argument the function will run (keeping your before, after and separator arguments) without writing in the <a> tags that means no hyperlinks :).

CODE PART2 [add to theme template]

<?php echo cc_get_the_term_list( $post->ID, 'tasks', '<!-- Before HTML --><b>Project Task(s):</b> ', ', ', '<br /><!-- After HTML -->', 1 ); ?>

OUTPUT DEMO
get-the-term-list-output2

Hope this helps you!

Share this post:

Priyanka Naskar Majumder - the Owner, Eyedea LAB. Graphic & Web Design Studio, lives in Kolkata, India. Interested in: Usability research, Graphic Design, Web Design & Development.


2 Comments


Leave a Reply

Your email address will not be published. Required fields are marked *

*

Allowed HTML tags: <strong> <em> <pre> <code> <a href="" title="">