• I am using the Salient wordpress theme which we are trying to re-use its ‘Project Attributes’ list.

    This is the code that outputs our list for each portfolio item:

    <?php
    	$project_attrs = get_the_terms( $post->ID, 'project-attributes' );
    	if (!empty($project_attrs)){ ?>
    		<div data-color="accent-color" data-animation="true" data-list-icon="icon-angle-right" class="nectar-fancy-ul">
    			<ul>
    				<?php
    					foreach($project_attrs as $attr){
    						echo '<li>' . $attr->name . '</li>';
    					}
    				?>
    			</ul>
    		</div>
    <?php } ?>

    Essentially we would like it to be listed hierarchically instead of A-Z but everything I try fails.

    Any help would be appreciated.

    Thanks heaps.

Viewing 1 replies (of 1 total)
  • I could not test this exactly, but the code here works when I use $project_attrs = get_terms('category');

    <?php
       $project_attrs = get_the_terms( $post->ID, 'project-attributes' );
       if (!empty($project_attrs)){
          $parents = array();
          foreach ($project_attrs as $term) {
             if ($term->parent <> 0) ++$parents[$term->parent]; // Identify all parents
          }
          ?>
          <div data-color="accent-color" data-animation="true" data-list-icon="icon-angle-right" class="nectar-fancy-ul">
             <?php $op = '<ul>';
                   foreach($project_attrs as $attr){
                      if ($attr->parent == 0) {
                         $op .= '<li>' . $attr->name . '</li>';
                         if (array_key_exists($attr->term_id, $parents)) {
                            add_term_children($attr->term_id, $depth=0, $op, $project_attrs, $parents);
                         }
                      }
                   }
             $op .= '</ul>';
             echo $op; ?>
          </div>
    <?php }
    
    function add_term_children($parent_id, $depth, &$op, $project_attrs, &$parents) {
       ++$depth;
       $sep = str_repeat(' -', $depth);
       foreach ($project_attrs as $attr) {
          if ($attr->parent == $parent_id) {
             $op .= '<li>' . "$sep $attr->name" . '</li>';
             if (array_key_exists($attr->term_id, $parents)) {
                add_term_children($attr->term_id, $depth, $op, $project_attrs, $parents);
             }
          }
       }
    }
    
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘get_the_terms Ordering’ is closed to new replies.