0

I have a theme with a widget that display posts from category and I wanna change it to posts from tags if it s possible.

Here is the code :

// NewsInPictures Widget
class NewsInPictures extends WP_Widget
{
    function NewsInPictures(){
    $widget_ops = array('description' => 'Shows your latest posts presented with their images.');
    $control_ops = array('width' => 200, 'height' => 300);
    parent::__construct(false,$name='[Voxis] News in pictures',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
    extract($args);

    $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
    $number_posts = $instance['number_posts'];
    $categories = $instance['categories'];

    echo $before_widget; 

    if ( $title != '' )
        echo $before_title . $title . $after_title;

    echo '
    <div class="photo-list">';
    $args = array();
    if(!empty($categories) )
        $args['category__in'] = $categories;
    $args['posts_per_page'] = $number_posts != 0 ? $number_posts : 20;
    $query = new WP_Query($args);
    while($query->have_posts() ) : $query->the_post(); global $post; ?>
        <div class='photo'>
            <?php 
            if(has_post_thumbnail() ) 
                $thumb = aq_resize(wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), 112, 112, true);
            else
                $thumb = get_template_directory_uri() . '/img/missing_56.png';
            ?>
            <a href="<?php the_permalink();?>" data-toggle="tooltip" title="<?php the_title();?>">
                <img src="<?php echo $thumb; ?>" alt="<?php the_title();?>" width="112px" height="112px" />
            </a>
        </div>
    <?php 
    endwhile; wp_reset_postdata(); 
    echo '</div>';

    echo $after_widget;
  }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
    $instance =  array();
    $instance['title'] = esc_attr($new_instance['title']);
    $instance['number_posts'] = (int)$new_instance['number_posts'];
    $instance['categories'] = $new_instance['categories'];

    return $instance;
  }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
    //Defaults
    $instance = wp_parse_args( (array) $instance, array('number_posts'=> 5, 'title' => '', 'categories' => array() ) );

    $title = esc_attr($instance['title']);
    $number_posts = (int) $instance['number_posts'];
    $categories = (array) $instance['categories'];

    echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
    echo '<p><label for="' . $this->get_field_id('number_posts') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number_posts') . '" name="' . $this->get_field_name('number_posts') . '" value="'. esc_textarea($number_posts)  . '" /></p>';
    $cats = get_categories('hide_empty=0');
    ?>
    <p>
        <label>Categories to include: </label> <br />
        <?php foreach( $cats as $category ) { ?>
            <label>
                <input type="checkbox" name="<?php echo $this->get_field_name('categories'); ?>[]" value="<?php echo $category->cat_ID; ?>"  <?php if(in_array( $category->cat_ID, $categories ) ) echo 'checked="checked" '; ?> /> 
                <?php echo $category->cat_name; ?>
            </label> <br />
        <?php } ?>
    </p> 
    <?php
  }

}// end NewsInPictures class
0

The main changes needed in order to use tags instead of categories would be:

In section one, change...

$args['category__in'] = $categories;

to

$args['tag__in'] = $categories;

And in the second section, change the cats loop:

$title = esc_attr($instance['title']);
$number_posts = (int) $instance['number_posts'];
$categories = (array) $instance['categories'];

echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title: ' . '</label><input id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" value="'. esc_textarea($title)  . '" /></p>';
echo '<p><label for="' . $this->get_field_id('number_posts') . '">' . 'Number of posts: ' . '</label><input id="' . $this->get_field_id('number_posts') . '" name="' . $this->get_field_name('number_posts') . '" value="'. esc_textarea($number_posts)  . '" /></p>';
$tags = get_tags('hide_empty=0');
?>
<p>
   <label>Tags to include: </label> <br />
   <?php foreach( $tags as $tag ) { ?>
      <label>
         <input type="checkbox" name="<?php echo $this->get_field_name('categories'); ?>[]" value="<?php echo $tag->term_id; ?>"  <?php if(in_array( $tag->term_id, $categories ) ) echo 'checked="checked" '; ?> /> 
         <?php echo $tag->name; ?>
      </label> <br />
   <?php } ?>
</p> 
<?php

Obviously, you can rename the variables and instance variables if wanted to reflect that you are now using tags instead of categories, but the changes I noted above will make it work properly.

  • i used the code above and i faced 1 probléme in the back office widget when i choose 1 tag to use and save all the tags r checked i cant check only 1 tag. thanks for help – Dolomats Jan 25 '15 at 15:05
  • 1
    $tag->term_ID should be $tag->term_id – Pieter Goosen Jan 25 '15 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.