1

I was trying to create a custom tag cloud widget that will look like this:

enter image description here

Also, when I tried including some new tags in my post then it's showing something like this (just one tag):

enter image description here

This is my functions.php

if(function_exists('register_sidebar')) {

    register_sidebar(
    array(
         'name' => __('Main Sidebar'),
         'id' => 'main-sidebar',
         'description' => __('The main sidebar area'),
         'before_widget' => '<div class="widget">',
         'after_widget' => '</div>',
         'before_title' => '<h2>',
         'after_title' => '</h2>'
   ));

}

/****************************************************/
/* Load Custom Widgets */  
/***************************************************/

require_once('functions/rh_tags.php');

This is my rh_tags.php

<?php 
class RH_Tags extends WP_Widget {
    function __construct() {
        $params = array(
            'name' => 'Creative : Tag Cloud Widget',
            'description' => 'Displays info of your blog'
            );
        parent:: __construct('RH_Tags','',$params);
    }

    public function form($instance) {
        //display our form in widget page
        extract($instance);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title') ?>">Title : </label>
            <input class="widefat" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" 
            value="<?php if(isset($title)) echo esc_attr($title); ?>" />
        </p>
        <?php
    }

    public function widget($args,$instance) {
        //displays our widget
        extract($args);
        extract($instance);
        echo $before_widget;
           echo $before_title .$title. $after_title;
           echo '<div class="label">';
           echo "<ul>";
                echo "<li>";
                echo the_tags(' ',' ');
                echo "</li>";
                echo "</ul>";
           echo '</div>';
        echo $after_widget;
    }
} 

add_action('widgets_init','rh_register_tags');
function rh_register_tags() {
    register_widget('RH_Tags');
}
  • The first question is about CSS. You will have to style your tag links accordingly. – Robert hue Nov 20 '14 at 9:30
  • Sorry, but this is a generic CSS question that does not invlove any Wordpress knowledge, hence this is regarded as off topic here. You might want to try stackoverflow.com :-). Thank you – Pieter Goosen Nov 20 '14 at 9:31
  • I've checked css but there z nothing in that.. – Raunak Hajela Nov 20 '14 at 9:34
  • @Peter, but why is it showing only one tag?? – Raunak Hajela Nov 20 '14 at 9:47
  • There are 5 tags in my wp post but its showing only one not all tags when i created new post and updated tags. – Raunak Hajela Nov 20 '14 at 9:51
0

What tags/terms/taxons do you get?

The the_tags( $before = null, $sep = ', ', $after = '' ) function is a wrapper for get_the_tag_list( $before = '', $sep = '', $after = '', $id = 0 ).

This function applies the filter the_tags on the get_the_term_list( 0, 'post_tag', $before = '', $sep = '', $after = '' ) (0 is the $id and $post_tag the $taxonomy argument) for the post_tag taxonomy and returns it. And get_the_term_list() is responsible for fetching the tags and building the MarkUp.

To fetch the terms, it uses get_the_terms( $id, $taxonomy ) internally. If you look at the internals of those functions (see links), it uses the get_post() if no $id was provided.

This indicates that you always will only get the tags/terms for a single post. To illustrate that, here're the first lines of the get_the_terms( $id, $taxonomy ) function:

function get_the_terms( $post, $taxonomy ) {
    if ( ! $post = get_post( $post ) )
        return false;

    $terms = get_object_term_cache( $post->ID, $taxonomy );
    if ( false === $terms ) {
        $terms = wp_get_object_terms( $post->ID, $taxonomy );
        wp_cache_add($post->ID, $terms, $taxonomy . '_relationships');
    }

As you can see, the second function in those wrappers provides no ID already, so it falls back to an ID of 0. And that makes if fall back to the global $post/\WP_Post object, which is the one from the main query (where \WP_Query->current_post points to). That can be a lot. At first it's the first post in the loop of the main query, which is run per default. Then, if the query does not get reset after the loop, it's the last post, etc. If you have later loops for e.g. done by plugins or your theme, it's a different post and so on.

Conclusion: If you fall back to the global post object, then you are using what ever the (unreliable) global provides you with.

What I would suggest is to use get_terms() instead as its output is easier to control and it does not rely on using data from a post - which you don't want.

Additional notes

Why not to use extract()? Because

  1. IDEs (like PHPStorm, Sublime, Aptana, etc.) do not recognize the origins/contents.
  2. It is unreliable as you never know if something is set or not. Use the normal array instead and rely on defaults and check it with empty() or isset():

    public function( Array $args )
    {
        $foo = isset( $args['foo'] )
            ? $args['foo']
            : 'my default';
        // work with `$foo`
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.