How to set “slug” link as active by default

I am working with a WordPress gallery plugin.

I would like the first link in the filter menu to automatically be active when the page loads. By default it loads ALL the foods from all the categories and it’s too many! Thanks.

the site is: http://dudits.flatstudio.hu

<?php
class Roll_Food extends WP_Widget {
    /**
     * Holds widget settings defaults, populated in constructor.
     *
     * @var array
     */
    protected $defaults;

    /**
     * Constructor
     *
     * @return Roll_Food
     */
    function __construct() {
        $this->defaults = array(     
        	'title'				=> '',
            'posts_number'         => '4',
            'category'      => '', 
            'show_filter' => true,
            'show_loadmore'          => true            
        );

        parent::__construct(
            'widget_food',
            esc_html__( 'Roll - Food', 'zupa' ),
            array(
                'classname'   => 'widget_food',
                'description' => esc_html__( 'Food Post.', 'zupa' )
            )
        );
    }

    /**
     * Display widget
     */
    function widget( $args, $instance ) {

        $instance = wp_parse_args( $instance, $this->defaults );
        extract( $instance );
        
        wp_enqueue_script('roll-waypoint');
        
        extract( $args ); 
              
        echo $before_widget;     

        echo '<div class="roll-menu">';

        //Build the filter navigation
        if ( $show_filter ) {
            $terms = get_terms('menus_category','orderby=name&hide_empty=0');            
            if ( count($terms) > 0 ) { 
                echo '<ul class="menu-filter"><li class="active"><a data-filter="*" href="#">' . esc_html__( 'ALL', 'zupa' ) . '</a></li>';                
                foreach ( $terms as $term ) {
                    $termname = strtolower($term->name);
                    $termname = str_replace(' ', '-', $termname);
                    echo '<li><a data-filter=".' . esc_attr( $termname ) . '" href="#" title="' . esc_attr( $term->name ) . '">' . esc_html($term->name) . '</a></li>';
                }
                echo '</ul>';//.food-filter
            }
        }    
        
        //Build the food items
        echo '<div class="food-wrap">'; 
        $terms_slug = wp_list_pluck( get_terms( 'menus_category','orderby=name&hide_empty=0'), 'slug' );   
       
        $tax = $terms_slug;  
        if ( !empty( $category ) ) {            
            $tax = $category;
        }     

        if ( get_query_var('paged') ) {
           $paged = get_query_var('paged');
        } elseif ( get_query_var('page') ) {
           $paged = get_query_var('page');
        } else {
           $paged = 1;
        }

        $query_args = array(
            'post_type' => 'food', 
            'posts_per_page' => intval($posts_number),
            'paged' => $paged,
            'tax_query' => array(
                array(
                    'taxonomy' => 'menus_category',   
                    'field'    => 'slug',                
                    'terms'    => $tax,
                ),               
            ),
        );
        
        $query = new WP_Query( $query_args );
        
        while ( $query->have_posts() ):
            $query->the_post();
            global $post;
            $id = $post->ID;
            $termsArray = get_the_terms( $id, 'menus_category' );
            $termsString = "";
             
            if ( $termsArray ) {
                foreach ( $termsArray as $term ) {
                    $termsString .= $term->slug.' ';
                }
            }

            if ( has_post_thumbnail() ) {
                $image_id = get_post_thumbnail_id();
                $image_url = wp_get_attachment_image_src($image_id,'roll-menu-images', true);
                ?>
                <div class="food-item <?php echo esc_attr( $termsString ); ?>">
                    <div class="food-thumb">
                        <a href="#">
                            <img src="<?php echo esc_url($image_url[0]); ?>" alt="<?php esc_html_e('image','zupa') ?>">
                        </a>
                    </div>
                    <div class="food-detail">                        
                        <?php the_title( '<h3 class="food-name">', '</h3>' ); ?>
                        <p class="food-info">
                            <?php echo wp_trim_words( get_the_content(), 20, '...' ); ?></p>
                        <p class="food-price">
                        <?php if (  roll_meta('price_food') != "" ) { echo esc_html( roll_meta('price_food') ); }                             
                        ?>
                        </p>
                        <ul class="food-rate">
                        <?php                         
                        $start_food = roll_meta('roll_rating_food'); 
                        for ($i = 1; $i <= 5; $i++) {
                            if( $i <= $start_food ) {
                                echo '<li><i class="fa fa-star"></i></li>'; 
                            } else {
                               echo '<li class="rate-hide"><i class="fa fa-star"></i></li>';   
                            }                            
                        }
                        ?>
                        </ul>
                    </div>
                </div><!-- /.food-item -->                
            <?php 
            }
            ?>
        <?php

        endwhile;
        wp_reset_postdata();      

        echo '</div>'; //.food-wrap
        if ( $show_loadmore ) {
        echo '<div class="roll-button-food">';
        echo '<a class="roll-button border" href="' . esc_url( get_pagenum_link($paged + 1) ) . '">';
        esc_html_e('LOAD MORE','zupa');
        echo '</a>';
        echo '</div>';//roll-button-group;        
        } 
        echo '</div>';//.end roll-menu;
        echo $after_widget;
    }
    /**
     * Update widget
     */
    function update( $new_instance, $old_instance ) {

        $instance               = $old_instance;

        $instance['posts_number']      = intval($new_instance['posts_number']) ;
        $instance['category']           = array_filter( $new_instance['category'] );
        $instance['show_filter']     = isset( $new_instance['show_filter'] ) ? (bool) $new_instance['show_filter'] : false;
        $instance['show_loadmore']     = isset( $new_instance['show_loadmore'] ) ? (bool) $new_instance['show_loadmore'] : false;       
        return $instance;
    }

    /**
     * Widget setting
     */
    function form( $instance ) {

        $instance = wp_parse_args( $instance, $this->defaults );
        $show_filter = isset( $instance['show_filter'] ) ? (bool) $instance['show_filter'] : false;
        $show_loadmore = isset( $instance['show_loadmore'] ) ? (bool) $instance['show_loadmore'] : false;        
        ?>        

        <p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'posts_number' ) ); ?>"><?php esc_html_e( 'Number item food', 'zupa' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'posts_number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'posts_number' ) ); ?>" type="number" size="2" value="<?php echo esc_attr($instance['posts_number']); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>"><?php esc_html_e( 'Select Category:', 'zupa' ); ?></label>
		   	<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'category' ) ); ?>[]">
		   		<option value=""<?php selected( empty( $instance['category'] ) ); ?>><?php esc_html_e( 'All', 'zupa' ); ?></option>
				<?php
				$categories = get_terms( 'menus_category','orderby=name&hide_empty=0' );
				foreach ( $categories as $cat ) {
					printf(
						'<option value="%s"%s>%s(%s)</option>',
						esc_attr( $cat->slug ),
						selected( in_array( $cat->slug, (array)$instance['category'] ) ),
						esc_attr( $cat->name ),
                        esc_attr( $cat->count )
					);
				}
				?>
		    </select>
		</p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked( $show_filter ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_filter' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_filter' ) ); ?>" />
            <label for="<?php echo esc_attr( $this->get_field_id( 'show_filter' ) ); ?>"><?php esc_html_e( 'Show Filter?', 'zupa' ); ?></label>
        </p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked( $show_loadmore ); ?> id="<?php echo esc_attr( $this->get_field_id( 'show_loadmore' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_loadmore' ) ); ?>" />
            <label for="<?php echo esc_attr( $this->get_field_id( 'show_loadmore' ) ); ?>"><?php esc_html_e( 'Show Load More ?', 'zupa' ); ?></label>
        </p>         

    <?php
    }

}

add_action( 'widgets_init', 'roll_register_food' );

/**
 * Register widget
 *
 * @return void
 * @since 1.0
 */
function roll_register_food() {
    register_widget( 'Roll_Food' );
}

Read more here: How to set “slug” link as active by default

Leave a Reply

Your email address will not be published. Required fields are marked *