Tags

From Habari Project

Jump to: navigation, search

Tags allow you to organize your posts into groups, much like the Gmail ‘labels’ system. A blog post can have more than one tag, and you can find a complete list of all the tags used in your blog on the Tags dashboard page.

Contents

Getting the Tags

To list all of the tags used on your site, you will need to either use a plugin like Fluffy Tag or HB-Cumulus or you will need to edit your theme's theme.php file and add the appropriate code.

The code you add will depend on what you want to show. There is an almost endless list of things you can do with the list of tags, so we'll stick with the simple matter of displaying all tags or displaying only the most popular tags.

All Tags

To display all your tags in your theme, you need to pull them from the database and assign them to an object the theme can use. The following excerpts will do just that. Add the code below to the add_template_vars() method in the theme's class defined in theme.php.

if ( !$this->template_engine->assigned( 'all_tags' ) ) {
  // List of all the tags
  $tags = DB::get_results('
    SELECT t.id AS id, t.term_display AS tag, t.term AS slug
    FROM {terms} t
    LEFT JOIN {object_terms} tp ON t.id=tp.term_id
    WHERE t.vocabulary_id = ?
    GROUP BY id, tag, slug
    ORDER BY tag ASC',
    array( Tags::vocabulary()->id )
  );
  $this->assign( 'all_tags', $tags );
}

This code will sort your tags in ascending order by tag name, ie alphabetic order from 'A' to 'Z'. You can change the order by changing the 'ASC' part of 'ORDER BY tag ASC' in each of the above snippets to 'DESC' which will order the tags in descending order.

Skip to Tags#Displaying_the_Tags to see how to display your tags now we've assigned them to '$all_tags'.

Only the Most Popular Tags

The above methods are great if you only have a few tags, but if you have a lot, you may want to only show the top 20 most popular tags or even order by popularity rather than tag name. To do this, use the following snippet of code:

if ( !$this->template_engine->assigned( 'all_tags' ) ) {
  // List of all the tags
  $tags = DB::get_results('
    SELECT t.id AS id, t.term_display AS tag, t.term AS slug,
    COUNT(tp.term_id) AS count
    FROM {terms} t
    LEFT JOIN {object_terms} tp ON t.id=tp.term_id
    WHERE t.vocabulary_id = ?
    GROUP BY id, tag, slug
    ORDER BY count DESC, tag ASC
    LIMIT 0, 20',
    array( Tags::vocabulary()->id )
  );
  $this->assign( 'all_tags', $tags );
}

This code will return only the 20 most popular tags ordered in descending order of usage and then in alphabetical order. Change the '20' in 'LIMIT 0, 20' to any value if you want more or less tags and change the 'DESC' or 'ASC' in 'ORDER BY count DESC, tag ASC' to 'ASC' or 'DESC' if you want to change the ordering of the results.

Now you have your tags, lets display them.

Displaying the Tags

There are many ways you can display your tags and the method is the same regardless of the method used to obtain the tags from the database. Essentially, you just need to iterate over the $all_tags array and display each item them as you wish. Here we'll demonstrate displaying all tags in an unordered list.

Add the following snippet of code to the template file corresponding to where you wish to display all your tags, for example sidebar.php if you want to show them in the sidebar:

<?php if( count( $all_tags ) > 0 ) { ?>
  <ul class="tags">
    <?php foreach($all_tags as $tag) {  ?>
      <li>
        <a href="<?php Site::out_url( 'habari' ); ?>/tag/<?php echo $tag->slug; ?>/" rel="tag" title="<?php echo $tag->tag; ?>">
          <?php echo $tag->tag; ?>
        </a>
      </li>
    <?php } ?>
  </ul>
<?php } ?>

You now have a list of tags in your theme and they're really to be styled using CSS to suit your needs.

If you want to display the tag title for the current page or how many posts have been tagged with a specific keyword you can also add the following snippet to tag.php

<?php echo Tags::post_count( $theme->tag); ?> articles tagged with: <?php echo $theme->tag; ?>
Personal tools
This is a cached copy of the requested page, and may not be up to date.

Sorry! This site is experiencing technical difficulties.
Try waiting a few minutes and reloading.

(Can't contact the database server: Access denied for user 'habari'@'localhost' to database 'habari_wiki' (localhost))


You can try searching via Google in the meantime.
Note that their indexes of our content may be out of date.