小部件无法使用4.3版本

时间:2015-08-21 04:30:04

标签: wordpress widget

感谢回复我非常感谢KārlisMillers,dingo-d现在我的小部件看起来如何。

<?php
    class ultimos_productos_widget_1 extends WP_Widget {

    function __construct() {

    parent::__construct(
        'ultimos_productos_widget', __( 'ultimos_productos_widget_1', 'tutsplustextdomain' ),
        array(
            'classname'   => 'ultimos_productos_widget_1',
            'description' => __( 'A basic text widget to demo the Tutsplus series on creating your own widgets.', 'tutsplustextdomain' )
            )
        );
    }

    public function widget($args,$instance){

        extract( $args );

        echo $before_widget = '<div class="sidebar woocommerce">' ;    
        ?>
        <h3 class="widget-title"> <?php _e('Ultimate Products', 'pruebadesarrollo'); ?></h3>
        <ul class="products">
            <?php
                $args = array( 
                    'post_type' => 'product', 
                    'posts_per_page' => 5, 
                    'product_cat' => 'marble',
                    'orderby' => 'ASC', 
                    /*'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => 'marble',
                                ),
                            ),*/
                    );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                    <li class="product">    
                        <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
                            <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
                            <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
                            <h3><?php the_title(); ?></h3>
                            <span class="price"><?php echo $product->get_price_html(); ?></span>                    
                        </a>
                        <?php if(is_user_logged_in() ){ ?>
                            <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                        <?php } ?>
                    </li>
            <?php endwhile; ?>
            <?php wp_reset_postdata() ?>
        </ul><!--/.products-->
        <?php
        echo $after_widget='</div>';
    }

    public function update($new_instance, $old_instance){
        // Función de guardado de opciones   
    }

    public function form($instance){
        // Formulario de opciones del Widget, que aparece cuando añadimos el Widget a una Sidebar
    }    
} 


function myplugin_register_widgets() {
    register_widget( 'ultimos_productos_widget_1' );
}

add_action( 'widgets_init', 'myplugin_register_widgets' );
?>

在更新a到wordpress 4.3之前一直在工作。

在wp_debug中显示此消息:

注意:从版本4.3.0开始,不推荐使用WP_Widget的被调用构造函数方法!使用

互联网研究我发现我应该改变下一行。

$this->WP_Widget('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);

parent::__construct('ultimos_productos_widget', "ultimos 5 proyectos", $widget_ops);

我不知道发生了什么事?

2 个答案:

答案 0 :(得分:0)

创建小部件后需要注册 刚刚上课:

register_widget('ultimos_productosaluminum_widget');

或者用wordpress风格:

function register_ultimos_widget() {
        register_widget('ultimos_productosaluminum_widget');
}
add_action( 'widgets_init', 'register_ultimos_widget' );

CODEX

了解详情

答案 1 :(得分:0)

感谢您的评论,

我使用建议修改了代码,这是我的自定义小部件。

class ultimos_productos_widget extends WP_Widget {

    function __construct() {
        $widget_ops = array('classname' => 'ultimos_productos_widget', 
                            'description' => "5 ultimos 5 productos por categoría" );
        parent::__construct('ultimos_productos_widget', "ultimos 5 productos por categoría", $widget_ops);
    }

    public function widget($args,$instance){
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title']);
        $ids   = $instance['ids'];
        $query_args = array(
            'post_status'    => 'publish',
            'post_type'      => 'product',
            'posts_per_page'  => 5,
            'orderby' => 'ASC',
            'tax_query' => array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'slug',
                            'terms'    => $ids,
                        ),
                    ),
        ); 

        $r = new WP_Query( $query_args );
        if ( $r->have_posts() ) {

            echo $before_widget;

            if ( $title )
                echo $before_title . $title . $after_title;

            echo '<ul class="product_list_widget">';

            while ( $r->have_posts()) {
                $r->the_post();
                global $product;
                 ?>
                    <li>
                        <a href="<?php echo esc_url( get_permalink( $product->id ) ); ?>" title="<?php echo esc_attr( $product->get_title() ); ?>">
                            <?php echo $product->get_image(); ?>
                            <?php echo $product->get_title(); ?>
                        </a>
                        <?php if ( ! empty( $show_rating ) ) echo $product->get_rating_html(); ?>
                        <?php if(is_user_logged_in() ){ ?>  
                            <?php echo $product->get_price_html(); ?>
                        <?php } ?>
                    </li>
                <?php
            }

            echo '</ul>';

            echo $after_widget;
        }

        wp_reset_postdata();

        echo $content;

    }

    public function form($instance){
        $title = (isset($instance['title'])) ? $instance['title'] : __( 'Products', 'ndb' );
        $ids = $instance['ids'];
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title :'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>"  name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('ids'); ?>"><?php _e('Category :'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('ids'); ?>"  name="<?php echo $this->get_field_name( 'ids' ); ?>" type="text" value="<?php echo esc_attr( $ids ); ?>" />
        </p>
    <?php

    }

    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['ids'] = ( ! empty( $new_instance['ids'] ) ) ? strip_tags( $new_instance['ids'] ) : '';
        return $instance;
    }    
}

function ultimos_productos() {
    register_widget( 'ultimos_productos_widget' );
}

add_action( 'widgets_init', 'ultimos_productos' );

这个插件可以很好地运行。

感谢。