WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] displaying custom taxonomies in particular order (20 posts)

  1. binga30
    Member
    Posted 1 year ago #

    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?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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>";
  3. binga30
    Member
    Posted 1 year ago #

    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.

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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>";
  5. binga30
    Member
    Posted 1 year ago #

    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

  6. binga30
    Member
    Posted 1 year ago #

    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.

  7. vtxyzzy
    Member
    Posted 1 year ago #

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

  8. binga30
    Member
    Posted 1 year ago #

    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

  9. vtxyzzy
    Member
    Posted 1 year ago #

    How do you determine the URL for a given item?

  10. binga30
    Member
    Posted 1 year ago #

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

  11. vtxyzzy
    Member
    Posted 1 year ago #

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

  12. binga30
    Member
    Posted 1 year ago #

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

  13. vtxyzzy
    Member
    Posted 1 year ago #

    How would you determine the URL in a program?

    I need to leave now - will continue tomorrow.

  14. binga30
    Member
    Posted 1 year ago #

    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.

  15. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  16. binga30
    Member
    Posted 1 year ago #

    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.

  17. vtxyzzy
    Member
    Posted 1 year ago #

    Each element in the array is a term object. You need to echo the properties of the object. To echo the name, you would use `echo $sorted_terms[0]->name;'

    Here is a Codex article showing all the fields:
    http://codex.wordpress.org/Function_Reference/get_terms#Return_Values

  18. binga30
    Member
    Posted 1 year ago #

    You are the king! Im doing a php and mysql course at the moment, but you have taught me something really useful in a matter of minutes.

    This will allow me to wrap my own href's to wherever I please.

    Do you have a site that I could donate to you?

  19. vtxyzzy
    Member
    Posted 1 year ago #

    Thanks for the offer, but I never accept donations. Please just donate to your favorite charity or just do a favor for someone else.

    If your problem has been solved, please use the dropdown on the right to mark this topic 'Resolved' so that anyone else with this question can see that there is a solution.

  20. binga30
    Member
    Posted 1 year ago #

    Thanks again, I like your website by the way.

Topic Closed

This topic has been closed to new replies.

About this Topic