更改wordpress小部件显示特定post_tag的帖子而不是特定类别

时间:2016-05-18 08:13:58

标签: php wordpress tags

我有一个小部件,根据所选类别显示帖子。 我可以从小部件设置页面的下拉菜单中选择我要显示其帖子的类别。 我想要做的是在该下拉菜单中显示post_tags,并根据所选的post_tag显示我的小部件中的帖子。

这是我的小部件代码:

<button [hidden]="!isPending" type="submit" class="btn btn-primary">Register</button>
<button [hidden]="isPending" class="btn btn-primary" disabled>Please wait...</button>

1 个答案:

答案 0 :(得分:0)

首先,您可以通过添加

获取post_tag下拉列表
'taxonomy'           => 'category',

运行wp_dropdown_categories。

然后在WP_Query类中_必须更改为tag__in。

这是工作代码:

class colormag_highlighted_posts_widget extends WP_Widget {

   function colormag_highlighted_posts_widget() {
      $widget_ops = array( 'classname' => 'widget_highlighted_posts widget_featured_meta', 'description' => __( 'Display latest posts or posts of specific category', 'colormag') );
      $control_ops = array( 'width' => 200, 'height' =>250 );
      parent::WP_Widget( false,$name= __( 'TG: Highligted Posts', 'colormag' ),$widget_ops);
   }

   function form( $instance ) {
      $tg_defaults['number'] = 4;
      $tg_defaults['type'] = 'latest';
      $tg_defaults['category'] = '';
      $instance = wp_parse_args( (array) $instance, $tg_defaults );
      $number = $instance['number'];
      $type = $instance['type'];
      $category = $instance['post_tag'];
      ?>
      <p>
         <label for="<?php echo $this->get_field_id('number'); ?>"><?php _e( 'Number of posts to display:', 'colormag' ); ?></label>
         <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" />
      </p>

      <p><input type="radio" <?php checked($type, 'latest') ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="latest"/><?php _e( 'Show latest Posts', 'colormag' );?><br />
       <input type="radio" <?php checked($type,'category') ?> id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="category"/><?php _e( 'Show posts from a category', 'colormag' );?><br /></p>

      <p>
         <label for="<?php echo $this->get_field_id( 'post_tag' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>:</label>
         <?php wp_dropdown_categories( array( 'show_option_none' =>' ','taxonomy' => 'post_tag','name' => $this->get_field_name( 'post_tag' ), 'selected' => $category ) ); ?>
      </p>
      <?php
   }

   function update( $new_instance, $old_instance ) {
      $instance = $old_instance;
      $instance[ 'number' ] = absint( $new_instance[ 'number' ] );
      $instance[ 'type' ] = $new_instance[ 'type' ];
      $instance[ 'post_tag' ] = $new_instance[ 'post_tag' ];

      return $instance;
   }

   function widget( $args, $instance ) {
      extract( $args );
      extract( $instance );

      global $post;
      $number = empty( $instance[ 'number' ] ) ? 4 : $instance[ 'number' ];
      $type = isset( $instance[ 'type' ] ) ? $instance[ 'type' ] : 'latest' ;
      $category = isset( $instance[ 'post_tag' ] ) ? $instance[ 'post_tag' ] : '';

      if( $type == 'latest' ) {
         $get_featured_posts = new WP_Query( array(
            'posts_per_page'        => $number,
            'post_type'             => 'post',
            'ignore_sticky_posts'   => true
         ) );
      }
      else {
         $get_featured_posts = new WP_Query( array(
            'posts_per_page'        => $number,
            'post_type'             => 'post',
            'tag__in'          => $category
         ) );
      }
      echo $before_widget;
      ?>
      <div class="widget_highlighted_post_area">
      <?php $featured = 'colormag-highlighted-post'; ?>
         <?php
         $i=1;
         while( $get_featured_posts->have_posts() ):$get_featured_posts->the_post();
            ?>
            <div class="single-article">
               <?php
               if( has_post_thumbnail() ) {
                  $image = '';
                  $title_attribute = get_the_title( $post->ID );
                  $image .= '<figure class="highlights-featured-image">';
                  $image .= '<a href="' . get_permalink() . '" title="'.the_title( '', '', false ).'">';
                  $image .= get_the_post_thumbnail( $post->ID, $featured, array( 'title' => esc_attr( $title_attribute ), 'alt' => esc_attr( $title_attribute ) ) ).'</a>';
                  $image .= '</figure>';
                  echo $image;
               } else { ?>
                  <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                     <img src="<?php echo get_template_directory_uri(); ?>/img/highlights-featured-image.png">
                  </a>
               <?php }
               ?>
               <div class="article-content">
                  <?php colormag_colored_category(); ?>
                  <h3 class="entry-title">
                     <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute();?>"><?php the_title(); ?></a>
                  </h3>
                  <div class="below-entry-meta">
                     <?php
                        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
                        $time_string = sprintf( $time_string,
                           esc_attr( get_the_date( 'c' ) ),
                           esc_html( get_the_date() )
                        );
                        printf( __( '<span class="posted-on"><a href="%1$s" title="%2$s" rel="bookmark"><i class="fa fa-calendar-o"></i> %3$s</a></span>', 'colormag' ),
                           esc_url( get_permalink() ),
                           esc_attr( get_the_time() ),
                           $time_string
                        );
                     ?>
                     <span class="byline"><span class="author vcard"><i class="fa fa-user"></i><a class="url fn n" href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" title="<?php echo get_the_author(); ?>"><?php echo esc_html( get_the_author() ); ?></a></span></span>
                     <span class="comments"><i class="fa fa-comment"></i><?php comments_popup_link( '0', '1', '%' );?></span>
                  </div>
               </div>

            </div>
         <?php
            $i++;
         endwhile;
         // Reset Post Data
         wp_reset_query();
         ?>
      </div>
      <?php echo $after_widget;
      }
}