I want to display a post taxonomy in a post template, but there only are 2 options to sort - Asc and Desc. But I need to sort them so the the primary taxonomy would be 1st, then the other taxonomy.
Hello there, I would like to follow up with you on yesterday's chat issue. Does the solution that I suggested worked for you? Do you have any further questions?
Hi,
It didn`t work.
The first code provided in that solution did work, but I had the same problem as that guy that I needed the name and not the link.
And the last code in the solution:
function list_hierarchical_terms($atts) {
global $post;
$taxonomy = $atts['taxonomy']; // change this to your taxonomy
$separator = '';
$out = '';
$term_ids = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
usort( $term_ids, 'cmp' );
foreach($term_ids as $term_id ) {
$term = get_term( $term_id, $taxonomy, OBJECT );
$out .= $separator . $term->name;
$separator = $atts['separator'];
}
return $out;
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
didn`t sort it hierarchical order.
The first code produces links to the taxonomy terms archives. You don't want links, you just need the terms, right?
In that case, I updated the code to use the strip_tags functions that will remove any HTML tags from the results. The code becomes:
function list_hierarchical_terms($atts) {
global $post;
$taxonomy = $atts['taxonomy'];
$separator = $atts['separator'];
$terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
if( $terms ) {
$terms = trim( implode( ',', (array) $terms ), ' ,' );
return strip_tags( wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none") );
}
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
And you can use it like this:
[display_post_tax_terms_hierarchical taxonomy="category" separator=" - "]
I tested it on my local setup with the regular posts and categories. Please test it from your side, and if it does not work, let me know what custom post type and custom taxonomy you are working on. Let me know if it would be ok for you to allow me temporary access to your website and let me check it further.
Thanks, this code works, but there still is a small problem - after the last category there is an extra seperator.
Instead of "First category - Second category" I get "First category - Second category -"
If it is needed I can give you access to my site
Please try this code:
function list_hierarchical_terms($atts) {
global $post;
$taxonomy = $atts['taxonomy'];
$separator = $atts['separator'];
$terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
if( $terms ) {
$terms = trim( implode( ',', (array) $terms ), ' ,' );
$results = wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
$results = rtrim( $results );
$results = rtrim( $results, $separator );
return strip_tags( $results );
}
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
If it does not work, I'll need to take a closer look at your website. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please let me know where do you show this.
My issue is resolved now. Huge thank you! By the way, maybe it could be added as a feature request, because I had the same problem with a view page where I wanted to sort hierarhicaly, but needed custom php. Atleast I think it makes a lot of sense to sort it like this and not Asc or Desc.