class Nieuws_Widget extends WP_Widget {
  function __construct() {
    parent::__construct(
      'nieuws_widget', // Base ID
      __('Laatste nieuws', 'text_domain'), // Name
      array( 'description' => __( 'Laatste nieuws berichten', 'text_domain' ), ) // Args
    );
  }
  public function widget( $args, $instance ) {
    echo $args['before_widget'];
    if ( !empty($instance['title']) ) {
      echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
    }
    $titel = get_field('titel', 'widget_' . $args['widget_id']);
    $tekst = get_field('tekst', 'widget_' . $args['widget_id']);
    $random = get_field('willekeurige_tonen', 'widget_' . $args['widget_id']);
    $aantal = get_field('aantal', 'widget_' . $args['widget_id']);
    $posttype = get_field('post_type', 'widget_' . $args['widget_id']);
    $categorie = get_field('thema', 'widget_' . $args['widget_id']);
    echo '<div class="row nwsblcks">';
    echo '<div class="col-12">';
    if($tekst): echo $tekst; endif;
    echo '</div>';
    if($categorie):
      $args = array(
      	 'post_type'		=> $posttype,
      	 'post__not_in' => array( get_the_ID()),
      	 'posts_per_page'	=> $aantal,
         'tax_query'              => array(
      		array(
      			'taxonomy'         => 'thema',
      			'terms'            => $categorie,
      			'field'            => 'slug',
      		),
      	),
      );
    else:
	      		$args = array(
	      			'post_type'		=> $posttype,
							'post__not_in' => array( get_the_ID()),
	      			'posts_per_page'	=> $aantal
	      		);
          endif;
	    		  $my_query = new WP_Query($args);
	    	    $count = 0;
	    	    while($my_query->have_posts()):
	    	    $count++;
	    	    $my_query->the_post();
						if (get_the_post_thumbnail_url()):
								$url = get_the_post_thumbnail_url();
						else:
								$url = get_field('nieuws_fallback_afbeelding','options');
						endif;
						$terms = get_the_terms( get_the_ID(), 'thema' );

						if ( $terms && ! is_wp_error( $terms ) ) :

						    $themas = array();

						    foreach ( $terms as $term ) {
										$themas[] = $term->slug;
						    }
						  endif;
							$term_list = wp_get_post_terms($post->ID, 'thema', ['fields' => 'all']);
							foreach($term_list as $term) {
							   if( get_post_meta($post->ID, '_yoast_wpseo_primary_thema',true) == $term->term_id ) {
									 if($term->slug == 'circulaire-economie'):
										 $kleur = "rd";
									 elseif($term->slug == 'energie-en-warmte'):
										 $kleur = "rnj";
									 elseif($term->slug == 'water-droogte-en-hitte'):
										 $kleur = "bl";
									else:
										$kleur = " ";
									endif;
							   }
							}; ?>
	            <div class="col-xs-12 col-sm-12 col-md-12 blck" style="margin-bottom: 0px;">
	              <a href="<?php the_permalink(); ?>">
	              <div class="nwsblck eq <?php echo $kleur; ?>" style="min-height: 50px;">
									<div class="icnhldr">
										<?php if (in_array("natuur-en-milieu", $themas)):
											echo '<img src="/wp-content/uploads/2019/09/natuur.png" alt="Natuur en milieu" />';
											endif;
											if (in_array("energie-en-warmte", $themas)):
												echo '<img src="/wp-content/uploads/2019/09/Energie-en-Warmte-Home-Knop.png" class="rnj" alt="Energie & warmte" />';
											endif;
											if (in_array("circulaire-economie", $themas)):
												echo '<img src="/wp-content/uploads/2019/09/circulaire-economie.png" class="rd" alt="Circulaire economie" />';
											endif;
											if (in_array("water-droogte-en-hitte", $themas)):
												echo '<img src="/wp-content/uploads/2019/09/water.png" class="bl" alt="Water, droogte & hitte" />';
											endif;
										?>
									</div>
									<h2><?php the_title(); ?></h2>
	              </div>
	            </a>
	            </div>
					<?php endwhile;  wp_reset_query();
    echo $args['after_widget'];
  }

  public function form( $instance ) {
    if ( isset($instance['title']) ) {
      $title = $instance['title'];
    }
    else {
      $title = __( 'Laatste nieuws', 'text_domain' );
    }
    ?>
    <p>
      <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></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( $title ); ?>">
    </p>
    <?php
  }

  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

    return $instance;
  }

}

add_action( 'widgets_init', function(){
  register_widget( 'Nieuws_Widget' );
});

function acf_load_color_field_choices( $field ) {
    $field['choices'] = array();
    $terms = get_terms( 'thema' );
    if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){

        foreach ( $terms as $term ) {
            $field['choices'][ $term->slug ] = $term->name;
        }
    }
    return $field;

}

add_filter('acf/load_field/name=thema', 'acf_load_color_field_choices');
Geef een reactie 0