[Resolved] List of terms for a post

Home Forums Support [Resolved] List of terms for a post

Home Forums Support List of terms for a post

Viewing 10 posts - 16 through 25 (of 25 total)
  • Author
    Posts
  • #2522724
    Fergal

    Hey David!

    Thanks for your help. I have the post terms displaying after using your snippet and short code. The terms are displaying better in the tablet and mobile layouts. I’ve provided a screenshot to show both methods in action. I’d like to move forward with the short code version. Are we able to do the following please:

    • Add space after commas
    • Remove the last comma when there are no more following terms
    • I’d prefer to show all terms from a taxonomy before showing terms from the next taxonomy. Currently, all terms are showing up in alphabetic order. Like in the original version I’d like to show the department terms, then the location terms, etc.

    Thanks,
    Fergal

    #2522743
    Fernando
    Customer Support

    Can you try this code instead?:

    function show_mutiple_terms() {
        global $post;
        $terms = wp_get_post_terms($post->ID, array( 'category','post_tag' ), array( "orderby" => "term_group" ) );
    	
        if ( $terms ) {
    		$i = 0;
            $len = count($terms);
            $html = '<div class="custom-terms">';
            foreach( $terms as $term ) {
    			if ($i == $len - 1) {
            		$html .= '<a href="' . esc_attr( get_term_link( $term ) ) . '">' . __( $term->name ) . '</a>';
        		} else {
    				$html .= '<a href="' . esc_attr( get_term_link( $term ) ) . '">' . __( $term->name ) . ', </a>';
    			}
                $i++;
            }
            $html .= '</div>';
    
        }
        return $html;
    }
    add_shortcode('display_terms', 'show_mutiple_terms' );

    Update the code for the Post types as instructed by David.

    #2522750
    Fergal

    Amazing thanks Fernando, works like a charm!

    #2522751
    Fernando
    Customer Support

    You’re welcome, Fergal!

    #2552054
    Fergal

    Hey Fernando,

    I’m not sure if we just got lucky initially when I tested the results of our latest code. But, now I’ve noticed the list of terms don’t seem to have any order at all when the initial goal was to output say all departments first, followed by locations, etc.

    Have any other ideas/methods that we can try please?

    Thanks,
    Fergal

    #2552380
    David
    Staff
    Customer Support

    Hi there,

    the code provided lists all Categories and All Tags.

    So when you say: all departments first, followed by locations, etc. – how are these set ?
    are they parent terms ?

    #2552973
    Fergal

    Hey David,

    Thanks for your reply. Departments, locations, experience-level are taxonomies registered to a custom post type. The only taxonomy with hierarchy (parent terms) is location and the hierarchy is country -> state -> city.

    My hope is for each post to list all terms from each taxonomy before moving on to another taxonomy.

    E.g., location term 1, location term 2, … location term n, department term 1, department 2, … dept term n, experience term 1

    Currently the list is pretty random like: location term 1, department term 1, location term 2, experience term, location term 3, …

    Hope this helps. Please let me know anything else I can clear up.

    Thanks,
    Fergal

    #2553128
    David
    Staff
    Customer Support

    Try this:

    function show_mutiple_terms() {
        $taxonomies = array( 'category', 'post_tag', 'custom_taxonomy' ); // Replace with your own array of taxonomies
        $terms_html = '';
    
        foreach ( $taxonomies as $taxonomy ) {
            $terms = get_the_terms( get_the_ID(), $taxonomy );
    
            if ( ! empty( $terms ) ) {
                foreach ( $terms as $term ) {
                    $term_class = sanitize_html_class( $taxonomy . '-' . $term->slug );
                    $terms_html .= '<li><a href="' . get_term_link( $term ) . '" class="' . $term_class . '">' . $term->name . '</a></li>';
                }
            }
        }
    
        if ( ! empty( $terms_html ) ) {
            return '<ul class="post-terms">' . $terms_html . '</ul>';
        } else {
            return '';
        }
    }
    add_shortcode('display_terms', 'show_mutiple_terms' );
    #2554674
    Fergal

    Thanks David! Your code solved my problem.

    #2555183
    David
    Staff
    Customer Support

    Glad to hear that

Viewing 10 posts - 16 through 25 (of 25 total)
  • You must be logged in to reply to this topic.