Support » Theme: ColorMag » If you want to Randomise Highlighted Posts

  • If you ever wanted to show random posts for say Highlighted Posts with a mod of the Highlighted Posts Widget Form, the code below works.

    Back up your site first!

    Find the Highlighted Posts section (From Line 336 of Widgets.Php) replace up to and including ” echo $before_widget; “

    /**
     * Highlighted Posts widget
     */
    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['category'];
          $random= $instance['random'];
          ?>
          <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( 'category' ); ?>"><?php _e( 'Select category', 'colormag' ); ?>:</label>
             <?php wp_dropdown_categories( array( 'show_option_none' =>' ','name' => $this->get_field_name( 'category' ), 'selected' => $category ) ); ?>
          </p>
          <p>
             <label for="<?php echo $this->get_field_id( 'random' ); ?>"><?php _e( 'Random Posts', 'colormag' ); ?>:</label>
             <input type="checkbox" name="<?php echo $this->get_field_name('random') ?>" <?php checked($instance['random'], 1) ?> />
           </p>
    
          <?php
       }
    
       function update( $new_instance, $old_instance ) {
          $instance = $old_instance;
          $instance[ 'number' ] = absint( $new_instance[ 'number' ] );
          $instance[ 'type' ] = $new_instance[ 'type' ];
          $instance[ 'category' ] = $new_instance[ 'category' ];
          $instance[ 'random' ] = $new_instance[ 'random' ];
    
          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[ 'category' ] ) ? $instance[ 'category' ] : '';
            if ($instance['random']) {
                $tmpvar = 'rand';
            }else{ $tmpvar = 'DESC';}
    
          if( $type == 'latest' ) {
             $get_featured_posts = new WP_Query( array(
                'posts_per_page'        => $number,
                'post_type'             => 'post',
                'orderby'               => $tmpvar,
                'ignore_sticky_posts'   => true
             ) );
          }
          else {
             $get_featured_posts = new WP_Query( array(
                'posts_per_page'        => $number,
                'post_type'             => 'post',
                'orderby'               => $tmpvar,
                'category__in'          => $category
             ) );
          }
          echo $before_widget;
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘If you want to Randomise Highlighted Posts’ is closed to new replies.