Support » Plugin: I Order Terms » Same requirement – used with get_the_terms() but cannot get it to work!

  • Hello,

    My client has the same requirement of displaying the terms in a certain order by post id, I came across this code snippet in the forum, should work fine but somehow I cannot make it to work yet. Sorry for the silly question but I’m new to WP and PHP: should I change $post_id and $taxonomy to the actual value? My current code is as below.

    //Forum code:

    function get_the_terms_sorted( $post_id, $taxonomy ) {
    	$terms = get_the_terms( $post_id, $taxonomy );
    	function cmp_by_custom_order( $a, $b ) {
    		return $a->custom_order - $b->custom_order;
    	}
    	if ( $terms ) usort( $terms, 'cmp_by_custom_order' );
    	return $terms;
    }

    // My code:

    <ul style="color:#999;">
              <?php
    			function get_the_terms_sorted( $post_id, $taxonomy ) {
                global $post;
    			$terms = get_the_terms( $post->ID, 'portfolio_category' );
    			function cmp_by_custom_order( $a, $b ) {
    			return $a->custom_order - $b->custom_order;
    		}
    			if ( $terms ) usort( $terms, 'cmp_by_custom_order' );
    		return $terms;
                if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
        foreach ( $terms as $term ) {
            echo '
    <li>' . '<a href="' . esc_url( get_term_link( $term ) ) . '">name ) ) . '">' . $term->name . '</a>' . '</li>
    ';
        }
        echo '';
    }
                }

    Thanks in advance!!

    https://wordpress.org/plugins/i-order-terms/

Viewing 1 replies (of 1 total)
  • Plugin Author Igor Jerosimic

    (@x64igor)

    Hi,

    I’m not sure what you mean sort terms by post id, did you mean sort by term id?

    I assume you need to put that function inside your functions.php, and then just call that function get_the_terms_sorted. Something like this:

    <ul style="color:#999;"><?php echo get_the_terms_sorted(); ?></ul>

Viewing 1 replies (of 1 total)
  • The topic ‘Same requirement – used with get_the_terms() but cannot get it to work!’ is closed to new replies.