WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] displaying custom taxonomies in particular order

[Resolved] displaying custom taxonomies in particular order

  • binga30

    @binga30

    Hello,

    I am using WordPress with the Premiumpress plugin for a directory site. The author has provided me with some code to output one of the custom taxonomies for the location of listings.
    <?php echo get_the_term_list( $post->ID, 'location', '', ', ', '' ); ?>
    This is great and gives the country, state and city, but unfortunately it is in alphabetical order. I need it to be in the country, state and city order like so:
    Australia >> Victoria >> Melbourne

    Would like to hear any tips or ideas for how I can arrange it like so?

Viewing 15 replies - 1 through 15 (of 19 total)
  • If the ‘location’ taxonomy is hierarchical to that ‘Australia’ is a child of a parent like ‘countries’, ‘Victoria’ is a child of a parent like ‘states’, and ‘Melbourne’ is a child of a parent like ‘cities’, then code similar to that below will work.

    It depends on all country terms having the same parent, all state terms having the same parent, and all city terms having the same parent.

    $id_array = array(65,0,78); // Put the Parent ids here in desired order
    $terms = get_the_terms($post->ID,'location');
    
    usort($terms, function($a, $b) use ($id_array) {
          $result = array_search($a->parent, $id_array) - array_search($b->parent, $id_array);
          if (!$result) $result = strcasecmp($a->name, $b->name);
          return $result;
       });
    
    $sep = '';
    $list = '';
    foreach ($terms as $term) {
       $list .= $sep . '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
       $sep = ', ';
    }
    echo "<p>$list</p>";

    Just so I understand correctly, your saying that each item such as Australia, Victoria and Melbourne would need their own parents?

    In my situation Australia is a parent, which has children QLD, NSW, VIC etc…. which they have children including all suburb/city names.

    Your setup is different than what I described. Here is a different approach that should work:

    $terms = get_the_terms($post->ID,'category');
    $sep = '';
    $list = '';
    $find_parent = 0;
    for( $i = 0; $i < sizeof($terms); ++$i) {
       foreach ($terms as $term) {
          if ($term->parent == $find_parent) {
             $find_parent = $term->term_id;
             $list .= $sep . '<a href="'.get_term_link($term).'">'.$term->name.'</a>';
             $sep = ', ';
          }
       }
    }
    echo "<p>$list</p>";

    Thanks for the suggestion, however I get an error when i insert that code into my page:

    Warning: Invalid argument supplied for foreach() in /home/content/79/8603079/html/squizzat/wp-content/themes/directorypress/template_directorypress/_item.php on line 63

    My mistake, I hadn’t input my taxonomy of location into the string, I left it as category.

    This code is nearly perfect, it does output in hierarchical order, but I would like to be able to put my own href around each item as they will be going off to different pages.

    In that case, just change the code in the line that adds each term to the list where it uses get_term_link().

    Hi Thanks for the quick reply.

    I understand I can insert my own href, but as its in a loop, any value I give it will apply to all items correct?

    What I need is to specify each items href individually.

    Thanks again

    How do you determine the URL for a given item?

    Well it depends if its top level, 2nd level or 3rd level item. Each level will be going to a level specific page.

    OK. Does it also depend on the value of the level, or just the level itself?

    Kind of, like if the 2nd level item is NSW, it will be going to a level specific page that contains NSW content.

    How would you determine the URL in a program?

    I need to leave now – will continue tomorrow.

    That’s the thing I haven’t worked out. It’s kind of getting away from my original question, but I was hoping to be able to output each term with its own code, thereby allowing me to assign links when I determine where they are going.

    I haven’t found out how to get the 2nd level term, or 3rd level term by itselt, without a link yet.

    I appreciate your assistance greatly.

    Here is one way to get the terms in order in an array.

    $terms = get_the_terms($post->ID,'category');
    $sorted_terms = array();
    $find_parent = 0;
    for( $i = 0; $i < sizeof($terms); ++$i) {
       foreach ($terms as $term) {
          if ($term->parent == $find_parent) {
             $find_parent = $term->term_id;
             $sorted_terms[] = $term;
          }
       }
    }

    The sorted_terms array now contains the terms in order. $sorted_terms[0] will contain the country term, $sorted_terms[1] will contain the state term, and $sorted_terms[2] will contain the city term.

    Thanks, this is exactly what I need. I just tried to echo $sorted_terms[0] though and got this error:

    Catchable fatal error: Object of class stdClass could not be converted to string on line 85.

    Line 85 contains my “echo $sorted_terms[0];” code.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘[Resolved] displaying custom taxonomies in particular order’ is closed to new replies.