Register new widget

时间:2011-10-03 作者:JasonDavis

我有一个widgetized区域,我正在尝试添加一个可以添加到我的widget区域的新widget,

我需要下面的代码作为小部件输出

<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>
我只需要一个标签列表(不是标签云)就可以与我的其他小部件进行排序,我尝试扩展小部件类,但失败了,请帮助

2 个回复
最合适的回答,由SO网友:JasonDavis 整理而成

我现在可以用下面的代码工作了,不确定这是不是最好的方法,但效果很好

/**
 * 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\');

SO网友:Tara

有关更多信息,请参阅本法典:http://codex.wordpress.org/Function_Reference/register_widget

结束

相关推荐

Why use widgets?

我对使用WordPress很陌生,我想知道使用小部件的好处是什么?看here 这听起来像是为那些不是程序员的人准备的,他们想在他们的网站上添加插件。对吗?或者小部件是否在某种程度上使站点更加健壮?