0

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

2
  • 1
    What do you mean by "failed"? Can you post the code you tried? Oct 3, 2011 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, 2011 at 1:27

2 Answers 2

1

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');
1
  • Mark your question as answered.
    – fuxia
    Feb 16, 2012 at 13:19
0

see this codex for more info: http://codex.wordpress.org/Function_Reference/register_widget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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