管理网站中的自定义分类术语没有为小部件保存

时间:2016-11-18 作者:solution.tom

我使用以下代码来显示下一个fixture的小部件。所有数据都显示正确,但自定义分类数据(季节和比赛)未保存在管理站点中。当我试图保存数据时,它会显示“所有季节”和“所有比赛”,而不是所需的季节和比赛。

代码:

<?php
/**
 * Posts Widget
 *
 * @package Smart Blog
 * @since 1.0
 */

add_action( \'widgets_init\', \'themepixels_register_widget_posts\' );
function themepixels_register_widget_posts() {
    register_widget( \'themepixels_widget_posts\' );
}

class themepixels_widget_posts extends WP_Widget {

    public function __construct() {
        $widget_ops = array( \'classname\' => \'posts_widget\', \'description\' => __( \'Fixture\', \'themepixels\' ) );
        parent::__construct( \'themepixels_posts\', __( \'Football Fixture\', \'themepixels\' ), $widget_ops );
    }

    public function widget( $args, $instance ) {
        extract($args);
        $title = apply_filters(\'widget_title\', $instance[\'title\']);
        $seasons    = $instance[\'seasons\']; 
        $competitions   = $instance[\'competitions\']; 
        $posts_order_by = $instance[\'posts_order_by\'];
        $posts_time_range = $instance[\'posts_time_range\'];
        $post_count = intval( $instance[\'post_count\'] );

        echo $before_widget;
        if( $title ) {
            echo $before_title . $title . $after_title;
        }

        $args = array(
            \'post_type\'             => \'football_fixture\',
            \'posts_per_page\'        => $post_count,
            \'orderby\'               => $posts_order_by,
            \'order\'                 => \'DESC\',
            \'meta_query\'            => array(
                                         array(
                                          \'key\'     => \'pb_match_status\',
                                          \'value\'   => \'fixture\'
                                        ),
            ),
        );

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

        <?php   while ($fixture_query->have_posts()) : $fixture_query->the_post(); ?>
      <div class="competition-result clearfix" id="post-<?php the_ID(); ?>">
        <?php
          $id     = get_the_ID();
          $date     = rwmb_meta( \'pb_match_date\',\'\', $post->ID);
          $time     = rwmb_meta( \'pb_match_time\',\'\', $post->ID );
          $team_home  = get_post_meta( get_the_ID(),  \'match_details_home_team\', true );
          $team_away  = get_post_meta( get_the_ID(),  \'match_details_away_team\', true );
          $team_home_score  = get_post_meta( get_the_ID(), \'pb_home_score\', true );
          $team_away_score  = get_post_meta(get_the_ID(), \'pb_away_score\', true );
        ?>

        <div class="widget-fixture-details">  
          <aside class="widget-competition-home-team-name-left">
            <a href="<?php echo get_link_football_team( $team_home ); ?>"><?php echo $team_home; ?></a>
          </aside>
          <aside class="widget-competition-team-logo-and-time">
              <button type="button" class="btn match-time-button">
              <span>
              <?php 
                  if ( $team_home_score ) { 
                     echo $team_home_score;  
                  }
              ?>
              </span>-
               <span>
              <?php 
                  if ( $team_away_score ) { 
                     echo $team_away_score;  
                  }
              ?>
              </span>

               </button>
          </aside>
          <aside class="widget-competition-home-team-name-right">
            <a href="<?php echo get_link_football_team( $team_away ); ?>"><?php echo $team_away; ?></a>
          </aside>
          <aside class="widget-football-match-centre-details">
           <div class="football-match-details" data-toggle="tooltip" data-placement="top" title="Click for details">
                <a href="<?php the_permalink(); ?>"><i class="fa fa-arrow-right" aria-hidden="true"></i></a>
            </div>
          </aside>
        </div>  
      </div><!--End post-->  

  <?php endwhile; ?>
   <?php wp_reset_postdata(); ?>

        <?php echo $after_widget; 
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        $instance[\'title\'] = strip_tags($new_instance[\'title\']);
        $instance[\'seasons\'] = $new_instance[\'seasons\'];
        $instance[\'competitions\'] = $new_instance[\'competitions\'];
        $instance[\'posts_order_by\'] = $new_instance[\'posts_order_by\'];
        $instance[\'post_count\'] = $new_instance[\'post_count\'];


        return $instance;
    }

    public function form( $instance ) {
        $defaults =  array(
            \'title\'                 => \'\',
            \'taxonomy\'              => \'0\',
            \'meta_key\'              => \'pb_match_date\',
            \'posts_order_by\'        => \'meta_value\',
            \'post_count\'            => 5,

        );

        $instance = wp_parse_args( (array) $instance, $defaults ); ?>

        <p>
            <label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e( \'Title:\', \'themepixels\' ); ?></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 $this->get_field_id( \'competitions\' ); ?>"><?php _e( \'Competitions:\', \'sntheme\' ); ?></label> 
                <select class="widefat" id="<?php echo $this->get_field_id( \'competitions\' ); ?>" name="<?php echo $this->get_field_name( \'competitions\' ); ?>">
                    <option value="">All Competitions</option>
                        <?php
                        $terms = get_terms("competition");
                        $count = count($terms);
                        if($count > 0) { foreach ( $terms as $term ) {
                            if($competitions == $term->term_id) {
                                echo \'<option selected="selected" value="\'.$term->term_id.\'">\'.$term->name.\'</option>\';
                            } else {
                                echo \'<option value="\'.$term->term_id.\'">\'.$term->name.\'</option>\';
                            }
                          } 
                        } 
                        ?>
                </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( \'seasons\' ); ?>"><?php _e( \'Seasons:\', \'sntheme\' ); ?></label> 
                <select class="widefat" id="<?php echo $this->get_field_id( \'seasons\' ); ?>" name="<?php echo $this->get_field_name( \'seasons\' ); ?>">
                    <option value="">All Seasons</option>
                <?php
                $terms = get_terms("session");
                $count = count($terms);
                if($count > 0) { foreach ( $terms as $term ) {
                    if($seasons  == $term->term_id) {
                        echo \'<option selected="selected" value="\'.$term->term_id.\'">\'.$term->name.\'</option>\';
                    } else {
                        echo \'<option value="\'.$term->term_id.\'">\'.$term->name.\'</option>\';
                    }
                  } 
                 } 
                ?>
                </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id(\'posts_order_by\'); ?>"><?php _e( \'Order by:\', \'themepixels\' ); ?></label>
            <select name="<?php echo $this->get_field_name(\'posts_order_by\'); ?>" id="<?php echo $this->get_field_id(\'posts_order_by\'); ?>" class="widefat">
                <option value="meta_value"<?php selected( $instance[\'posts_order_by\'], \'meta_value\' ); ?>><?php _e( \'Date\', \'themepixels\' ); ?></option>
            </select>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id(\'post_count\'); ?>"><?php _e( \'Number of posts to show:\', \'themepixels\' ); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id(\'post_count\'); ?>" name="<?php echo $this->get_field_name(\'post_count\'); ?>" type="text" value="<?php echo intval( $instance[\'post_count\'] ); ?>" />
        </p>

    <?php
    }
}

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

在您的小部件表单的季节和比赛foreach循环中,您使用了

if($competitions == $term->term_id) {......}
以及

if($seasons  == $term->term_id) {.......}
但这是错误的,请使用以下代码代替这些代码:

if($instance[\'competitions\'] == $term->term_id) {}
以及

if($instance[\'seasons  \'] == $term->term_id) {}

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'