web-dev-qa-db-fra.com

Pourquoi le widget de publication populaire wordpress affiche-t-il un index non défini?

Pendant la création d'un widget de publication populaire, je vois cette notification:

Notice: Undefined index: popular_posts_order_by in /hermes/bosnaweb09a/b2039/ipg.tutorialcoursecom/arifhossin/wp-content/themes/testtheme/framework/widgets/pb-popular-post.php on line 26.

Voici mon code ... Quelqu'un peut-il résoudre ce problème?

<?php


add_action( 'widgets_init', 'personalblog_popular_posts_widget' );
    function personalblog_popular_posts_widget() {
        register_widget( 'Personalblog_Popular_Posts' );
    }

    class Personalblog_Popular_Posts extends WP_Widget{
        // Initialize the widget
        public function __construct() {
            parent::__construct(
                'personalblog_popular_posts_widget', esc_html__('Strawberry: Popular Posts Widget','strawberry'), array('description' => esc_html__('Strawberry:A widget that shows Polupar Posts', 'strawberry')));  
        }

        // Output of the widget
        public function widget( $args, $instance ) {
            extract($args);
            $title = apply_filters('widget_title', $instance['title']);
            $popular_posts_order_by = $instance['popular_posts_order_by'];
            $poular_posts_time_range = $instance['poular_posts_time_range'];
            $num_fetch = $instance['num_fetch'];

            // Opening of widget
            echo $before_widget;

            // Open of title tag
            if( !empty($title) ){ 
                echo $before_title . $title . $after_title; 
            }
            $args = array(
            'post_type'             => 'post',
            'posts_per_page'        => $num_fetch,
            'ignore_sticky_posts'   => true,
            'no_found_rows'         => true,
            'post_status'           => 'publish',
            'orderby'               => $popular_posts_order_by,
            'order'                 => 'DESC',
            'date_query' => array(
                array(
                    'after'  => $poular_posts_time_range,
                ),
            ),
        );

        $posts_query = new WP_Query($args); ?>

        <?php if ( $posts_query->have_posts() ) { ?>
            <div class="pb-recent-posts-widget-section clearfix">
                <ul class="pb-recent-post-widget-list">
                    <?php while ( $posts_query->have_posts() ) : $posts_query->the_post(); ?>
                        <li id="post-<?php the_ID(); ?>">
                                <figure class="pb-recent-post-widget-thumbnail-section">
                                    <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                        <?php the_post_thumbnail( 'widget-post-image' ); ?>
                                    </a>
                                </figure>

                            <div class="pb-recent-post-widget-section">
                                <h2 class="pb-recent-post-widget-title"><a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
                                <?php if( pb_theme_options('enable_widget_meta') == '1' ) { ?>
                                    <div class="pb-recent-post-widget-meta clearfix">
                                        <ul>
                                        <?php if( pb_theme_options('widget_meta_date') == '1' ) { ?>
                                            <li><i class="fa fa-clock-o"></i><span><?php echo get_the_date(); ?></span></li>
                                            <?php } ?>
                                            <?php if( pb_theme_options('widget_meta_comments') == '1' ) { ?>
                                            <li><i class="fa fa-comments">
                                                <span>
                                                <?php printf( _nx( '', '%1$s', 'comments title', get_comments_number(), 'personalblog' ), number_format_i18n( get_comments_number() ) ); ?>
                                                </span>
                                              </i>
                                           </li>
                                           <?php } ?>
                                        </ul>
                                        <ul>
                                        <?php if( pb_theme_options('widget_meta_category') == '1' ) { ?>
                                            <li><p class="category"><i class="fa fa-folder-open"> </i> <?php the_category(', '); ?></p></li>
                                            <?php } ?>
                                        <?php if( pb_theme_options('widget_meta_likes') == '1' ) { ?>
                                           <li><?php echo getPostLikeLink( get_the_ID() )?> </li>
                                           <?php } ?>

                                        </ul>
                                    </div>
                                <?php } ?>
                            </div>
                        </li>
                    <?php endwhile; ?>
                </ul>
            </div>
        <?php }
        wp_reset_postdata();

        // Closing of widget

        echo $after_widget;
        }

        // Widget Form
        public function form( $instance ) {

            $defaults =  array(
            'title'         => 'Popular Posts',
            'popular_posts_order_by'        => 'post_views_count',
            'num_fetch'         => '5',
            'poular_posts_time_range'       => '0'

        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
            <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e( 'Title:', 'strawberry' ); ?></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( $instance['title'] ); ?>" />
        </p>

            <p>
                <label for="<?php echo esc_attr ($this->get_field_id('popular_posts_order_by')); ?>"><?php esc_html_e( 'Order by:', 'strawberry' ); ?></label>
                <select name="<?php echo esc_attr ($this->get_field_name('popular_posts_order_by')); ?>" id="<?php echo esc_attr ($this->get_field_id('popular_posts_order_by')); ?>" class="widefat">
                    <option value="post_views_count"<?php selected( $instance['popular_posts_order_by'], 'post_views_count' ); ?>><?php esc_html_e( 'View Count', 'strawberry' ); ?></option>
                    <option value="comment_count"<?php selected( $instance['popular_posts_order_by'], 'comment_count' ); ?>><?php esc_html_e( 'Most commented', 'strawberry' ); ?></option>
                </select>
           </p>

            <p>
                <label for="<?php echo esc_attr ($this->get_field_id('poular_posts_time_range')); ?>"><?php esc_html_e( 'Time Range:', 'strawberry' ); ?></label>
                <select name="<?php echo esc_attr ( $this->get_field_name('poular_posts_time_range')); ?>" id="<?php echo esc_attr ( $this->get_field_id('poular_posts_time_range')); ?>" class="widefat">
                    <option value="0"<?php selected( $instance['poular_posts_time_range'], '0' ); ?>><?php esc_html_e( 'All time', 'strawberry' ); ?></option>
                    <option value="1 year ago"<?php selected( $instance['poular_posts_time_range'], '1 year ago' ); ?>><?php _e( 'This year', 'strawberry' ); ?></option>
                    <option value="1 month ago"<?php selected( $instance['poular_posts_time_range'], '1 month ago' ); ?>><?php esc_html_e( 'This month', 'strawberry' ); ?></option>
                    <option value="1 week ago"<?php selected( $instance['poular_posts_time_range'], '1 week ago' ); ?>><?php esc_html_e( 'This week', 'strawberry' ); ?></option>
                    <option value="1 day ago"<?php selected( $instance['poular_posts_time_range'], '1 day ago' ); ?>><?php esc_html_e( 'Past 24 hours', 'strawberry' ); ?></option>
                </select>
            </p>
            <p>
            <label for="<?php echo $this->get_field_id('num_fetch'); ?>"><?php esc_html_e( 'Number of posts to show:', 'strawberry' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('num_fetch'); ?>" name="<?php echo $this->get_field_name('num_fetch'); ?>" type="text" value="<?php echo intval( $instance['num_fetch'] ); ?>" />
           </p>

            <?php
        }

        // Update the widget
        public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['popular_posts_order_by'] = $new_instance['popular_posts_order_by'];
        $instance['poular_posts_time_range'] =$new_instance['poular_posts_time_range'];
        $instance['num_fetch'] = $new_instance['num_fetch'];
        return $instance;
    }



    }
    ?>

Merci

2
bdtheme

Cet avis apparaît lorsque vous essayez d'accéder à un index non défini d'un tableau.

Pour résoudre ce problème, vous devez vérifier si l'index existe avant de pouvoir y accéder. Pour cela, vous pouvez utiliser isset() ou array_key_exists() :

J'aurais besoin de voir la ligne de code exacte à laquelle l'avis fait référence ( ligne 26 ) afin de vous fournir une réponse exacte à votre code. Voici un exemple général:

// Options 1: isset()
$value = isset($array['my_index']) ? $array['my_index'] : '';
// Options 2: array_key_exists()
$value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
4