Support » Fixing WordPress » How to display custom taxonomy tags OUTSIDE the loop

  • Resolved perthmetro

    (@perthmetro)


    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 🙂

Viewing 15 replies - 1 through 15 (of 24 total)
  • 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 />";
    }

    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?

    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>';
    }

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

    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.
    }

    Perfect… you read my mind!

    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

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

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

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

    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.
    } ?>

    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.
    } ?>

    Ahhh, cheers 🙂

    The hyphen is still there.

    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.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘How to display custom taxonomy tags OUTSIDE the loop’ is closed to new replies.