Forums

Displaying taxonomies (5 posts)

  1. omerosen
    Member
    Posted 1 year ago #

    I have created taxonomies by the names Company, Country and City.
    Now I want to display it on the index page like that:

    Listed companies: HP, IBM, Apple ...
    Listed countries: USA, Sweden, Canada ...
    Listed cities: NY, Stockholm, Toronto ...

    The first 3 tags are supposed to be the most popular in each taxonomy.
    I guess it can be done only by hard coding into the theme, but how can I do it?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You should be able to use the get_terms() function.

    It would go something like this (UNTESTED):

    <?php $companies = get_terms('Company','orderby=count&order=DESC&fields=names');
    if ($companies) {
       echo '<p>Listed companies: ';
       $sep = '';
       foreach ($companies as $company) {
          echo $sep . $company;
          $sep = ', ';
       }
       echo '</p>';
    }?>
  3. omerosen
    Member
    Posted 1 year ago #

    the code works great except for that it doesn't link the tags, only showing them as a text.
    Also, is there a way to limit the nu,ber of shown tags, for example to the most popular 5?

  4. vtxyzzy
    Member
    Posted 1 year ago #

    AFAIK, there is no way to create permalinks for custom taxonomies without a plugin - therefore, no links.

    As for limiting the number, add the number parameter:

    <?php $companies = get_terms('Company','orderby=count&order=DESC&fields=names&number=5');
    if ($companies) {
       echo '<p>Listed companies: ';
       $sep = '';
       foreach ($companies as $company) {
          echo $sep . $company;
          $sep = ', ';
       }
       echo '</p>';
    }?>
  5. omerosen
    Member
    Posted 1 year ago #

    Great, thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags