Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a widgetized area and I am trying to add a new widget that can be added to my widget area,

I need the code below to be the widget output

<ul>
<?php
$args = array( 'taxonomy' => 'post_tag' );
$terms = get_terms('post_tag', $args);
$count = count($terms); $i=0;
if ($count > 0) {
    foreach ($terms as $term) {
        $i++;
        $term_list .= '<li><a href="/tag/' . $term->slug . '" title="' . s__('View all post filed under %s', 'my_localization_domain'), name) . '">' . $term->name . '</a></li>';
    }
    echo $term_list;
}
?>
</ul>

I just need a tag list (not tag cloud) to be sortable with my other widgets, I tried extending the widget class but failed, please help

share|improve this question
1  
What do you mean by "failed"? Can you post the code you tried? –  Devin Humbert Oct 3 '11 at 0:28
    
@Devin Humbert I mean if I take an existing widget from the core files and inbetween echo $before_widget; and echo $after_widget; add the code above, it just does not work –  jasondavis Oct 3 '11 at 1:27
add comment

2 Answers

up vote 1 down vote accepted

I got it working now with the code below, not sure if it is the best way but it works good

/**
 * Tag LIST widget class
 */
class WP_Widget_Tag_List extends WP_Widget
{

    function __construct()
    {
        $widget_ops = array('description' => __("Your most used tags in List format"));
        parent::__construct('tag_list', __('Tag List'), $widget_ops);
    }

    function widget($args, $instance)
    {
        extract($args);
        $current_taxonomy = $this->_get_current_taxonomy($instance);
        if (!empty($instance['title'])) {
            $title = $instance['title'];
        } else {
            if ('post_tag' == $current_taxonomy) {
                $title = __('Tags');
            } else {
                $tax = get_taxonomy($current_taxonomy);
                $title = $tax->labels->name;
            }
        }
        $title = apply_filters('widget_title', $title, $instance, $this->id_base);

        echo $before_widget;
        if ($title)
            echo $before_title . $title . $after_title;
        echo '<div class="taglist">';
        //wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
        echo '<ul>';
        $args = array('taxonomy' => 'post_tag');
        $terms = get_terms('post_tag', $args);
        $count = count($terms);
        $i = 0;
        if ($count > 0) {
            foreach ($terms as $term) {
                $i++;
                $term_list .= '<li><a href="/tag/' . $term->slug .
                    '" title="View all post filed under ' . $term->name . '">' . $term->name .
                    '</a></li>';
            }
            echo $term_list;
        }

        echo '</ul>';
        echo "</div>\n";
        echo $after_widget;
    }

    function update($new_instance, $old_instance)
    {
        $instance['title'] = strip_tags(stripslashes($new_instance['title']));
        $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
        return $instance;
    }

    function form( $instance ) {
        $current_taxonomy = $this->_get_current_taxonomy($instance);
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
        <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
        <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
        <?php foreach ( get_object_taxonomies('post') as $taxonomy ) :
                $tax = get_taxonomy($taxonomy);
                if ( !$tax->show_tagcloud || empty($tax->labels->name) )
                    continue;
                    ?>
                    <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
        <?php endforeach; ?>
        </select></p><?php
    }

    function _get_current_taxonomy($instance) {
        if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
            return $instance['taxonomy'];

            return 'post_tag';
    }
}
register_widget('WP_Widget_Tag_List');
share|improve this answer
    
Mark your question as answered. –  toscho Feb 16 '12 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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