Written by Anonymous
//サイドナビにおすすめタグを表示 class MySideWidgetRecommend extends WP_Widget{ function __construct(){ parent::__construct( 'my_side_widget_recommend', 'おすすめタグのウィジェット', array('description' => 'おすすめタグを表示') ); } //ウィジェットの表示 public function widget($args, $instance){ if ( is_singular() ){ $before_html = '<aside id="navi_entries" class="widget widget-sidebar widget-sidebar-standard widget_navi_entries"><h3 class="widget-sidebar-title widget-title">オススメ記事</h3><div class="navi-entry-cards widget-entry-cards no-icon card-title-bold"> '; $after_html = '</div></aside> '; echo $before_html; $args = array( 'post_type' => 'post', //'tag' => 'recommend',//タグのスラッグ名 'category_name' => 'recommend',//カテゴリのスラッグ名 'orderby' => 'rand',//ランダム 'posts_per_page' => 10//10件まで ); $the_query = new WP_Query( $args ); $count = 0; if ( $the_query->have_posts() ) { while( $the_query->have_posts() ) { $the_query->the_post(); $count++; set_query_var( 'count', $count ); get_template_part('tmp/related-entry-card'); } wp_reset_postdata(); } echo $after_html; } } } add_action( 'widgets_init', function(){ register_widget('MySideWidgetRecommend'); } );