WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display custom taxonomy tags OUTSIDE the loop (25 posts)

  1. Pete
    Member
    Posted 1 year ago #

    I have a page with 100 custom tags assigned to it. These tags are assigned to the custom taxonomy 'electrician'.

    I have a template page called taxonomy-electrician.php

    This template page simply shows the below, depending on the custom tag...

    <?php single_tag_title(); ?> is an electrician

    In effect it makes me 100 different pages (1 for each custom tag)

    I have another group of 50 custom tags from a different custom taxonomy (suburbs).
    NB: This group of 50 tags are not related in any way way to the 100 'electrician' custom tags

    On my taxonomy-electrican page I also want to show a list of 10 random custom tags from the other custom taxonomy (suburbs).

    In effect I want the taxonomy-electrician.php page to look like this...

    Bill is an electrician

    [10 random tags from 'suburbs']

    Thanks heaps.

    I have search high and low. If I didn't have so little hair I'd pull it out :)

  2. vtxyzzy
    Member
    Posted 1 year ago #

    I think the get_terms() function will do what you want:

    $suburbs = get_terms('suburbs','hide-empty=0&orderby=id');
    shuffle($suburbs);
    foreach ( $suburbs as $suburb ) {
       if( ++$count > 10 ) break;
       echo "COUNT:$count ID:$suburb->term_id NAME:$suburb->name SLUG:$suburb->slug <br />";
    }
  3. Pete
    Member
    Posted 1 year ago #

    You, my friend, are a legend! I have been looking and asking all over the web for this answer for some time now and you've cracked it... you are officially my best friend for the month of August... thanks :)

    Can I push the friendship and ask how I'd put in a separator so the last name doesn't have a suffix?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    What do you want them to link to? Can you give an example of a link?

    As a guess, I think this might be what you want:

    $suburbs = get_terms('suburb','hide-empty=0&orderby=id');
    shuffle($suburbs);
    foreach ( $suburbs as $suburb ) {
       if( ++$count > 10 ) break;
       echo '<li><a href="'.get_term_link($suburb).'">'.$suburb->name.'</a>';
    }
  5. Pete
    Member
    Posted 1 year ago #

    yeah sorry, a link to the tag... can a separator be incorporated into this? Ta.

  6. vtxyzzy
    Member
    Posted 1 year ago #

    I'm not too good at mind reading. What kind of separator? Show me how you want it to look.

    Here is one way to do it.

    $suburbs = get_terms('suburb','hide-empty=0&orderby=id');
    shuffle($suburbs);
    foreach ( $suburbs as $suburb ) {
       if( ++$count > 10 ) break;
       echo $sep . '<a href="'.get_term_link($suburb).'">'.$suburb->name.'</a>';
    $sep = ' | ';  // Put your separator here.
    }
  7. Pete
    Member
    Posted 1 year ago #

    Perfect... you read my mind!

  8. vtxyzzy
    Member
    Posted 1 year ago #

    Did you actually try the code I posted? The separator does not appear before the first entry, or after the last entry. Actual output on my test site:

    $10,000 - $60,000 | Resort | Fort Wayne | One under Gull | Odd Pages | Gosh another test | bcategory | Cities | Low Price | $100,000 - $149,999
  9. Pete
    Member
    Posted 1 year ago #

    Yeah sorry I pasted another set of the code in front of it as well!... thanks for an awesome piece of code.

  10. Pete
    Member
    Posted 1 year ago #

    I'm not sure if it's me but at the start of the tags there is a hyphen.

  11. vtxyzzy
    Member
    Posted 1 year ago #

    I don't know what caused that. There is no hyphen in the code I sent.

  12. Pete
    Member
    Posted 1 year ago #

    This is what I'm using and i'm pretty sure there's something in the code causing the hyphen - i've tried the code on different templates and the hyphen appears each time.

    <?php $electrician = get_terms('electrician','hide-empty=0&orderby=id');
    shuffle($electrician);
    foreach ( $electrician as $electrician ) {
    if( ++$count > 70 ) break;
    echo $sep . '<a href="'.get_term_link($electrician).'">'.$electrician->name.'</a>';
    $sep = ', ';  // Put your separator here.
    } ?>
  13. vtxyzzy
    Member
    Posted 1 year ago #

    You are using the same variable for the electricians array as the individual electrician. Pay attention to the plurals:

    <?php $electricians = get_terms('electrician','hide-empty=0&orderby=id');
    shuffle( $electricians );
    foreach ( $electricians as $electrician ) {
       if( ++$count > 70 ) break;
       echo $sep . '<a href="'.get_term_link($electrician).'">'.$electrician->name.'</a>';
       $sep = ', ';  // Put your separator here.
    } ?>
  14. Pete
    Member
    Posted 1 year ago #

    Ahhh, cheers :)

  15. Pete
    Member
    Posted 1 year ago #

    The hyphen is still there.

  16. vtxyzzy
    Member
    Posted 1 year ago #

    There is no hyphen in the code I supplied. When I copy the code you showed above and run it, I get no hyphen. You must look in the code before what you showed to find it.

  17. Pete
    Member
    Posted 1 year ago #

    The only thing i can think of is i have more than one instance of it... not in the same templates but in 2 different templates?

    I'm using the above code?

    This my custom taxonomy set up screenshot

    Thanks again.

  18. Pete
    Member
    Posted 1 year ago #

    I found this piece of code, i'm having trouble adding the number limit, random and separator arguments...

    <?php
    $taxonomy = 'electrician';
    $queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
    $terms = get_terms($taxonomy);
    if ($terms) {
      foreach($terms as $term) {
        echo $sep . '<a href="' . $term->slug . '">' . $term->name .'</a>';
    $sep = ', ';  // Put your separator here.
      }
    }
    ?>
  19. vtxyzzy
    Member
    Posted 1 year ago #

    <?php
    $taxonomy = 'electrician';
    $limit = 10;
    $queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
    $terms = get_terms($taxonomy);
    shuffle($terms);
    if ($terms) {
      foreach($terms as $term) {
        if ( ++$count > $limit) break;
        echo $sep . '<a href="' . $term->slug . '">' . $term->name .'</a>';
    $sep = ', ';  // Put your separator here.
      }
    }
    ?>
  20. Pete
    Member
    Posted 1 year ago #

    Cheers... you wouldn't believe it, I still get that dang hyphen with this new one as well!

  21. vtxyzzy
    Member
    Posted 1 year ago #

    OK. To prove whether or not it is in this code, change this:

    shuffle($terms);

    to this:

    shuffle($terms);
    echo '<p> start </p>';

    If the hyphen comes before ' start ' it must be in other code because ' start ' will be the first thing echoed in this code.

  22. Pete
    Member
    Posted 1 year ago #

    deleted comment

  23. Pete
    Member
    Posted 1 year ago #

    OK the start is before the hyphen

  24. vtxyzzy
    Member
    Posted 1 year ago #

    It looks like you used $sep in some earlier code and did not reset it.

    Change this:

    shuffle($terms);
    echo '<p> start </p>';

    to this:

    shuffle($terms);
    $sep = '';
  25. Pete
    Member
    Posted 1 year ago #

    Awesome - That did the trick... i'll have a look into my plugins to see if they're affecting it. Thanks for you patience. :)

Topic Closed

This topic has been closed to new replies.

About this Topic