为什么WordPress流行的POST小工具显示未定义的索引?

时间:2016-08-26 作者:bdtheme

在制作流行贴子的过程中,我看到了以下通知:

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.
以下是我的代码。。谁能解决这个问题?

<?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;
    }



    }
    ?>
谢谢

1 个回复
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成

当您尝试访问数组的未定义索引时,会出现此通知。

要解决此问题,需要先检查索引是否存在,然后再访问它。为此,您可以使用isset()array_key_exists():

我需要查看通知所指的确切代码行(第26行),以便为您提供代码的确切答案。以下是一个通用示例:

// 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\'] : \'\';

相关推荐

如何在WooCommerce_Account_Orders函数中传递$CURRENT_PAGE?

woocommerce功能woocommerce_account_orders($current_page) 已调用1个参数$current_page. 该函数通过调用woocommerce_account_orders_endpoint 通过以下挂钩-add_action( \'woocommerce_account_orders_endpoint\', \'woocommerce_account_orders\' );然而,在上述挂钩中,$current_page 未在函数中作为参数传递。情况如何$c