-
AuthorPosts
-
October 19, 2014 at 8:31 am #127973
Hi,
How do i add Filter posts by multiple checkbox categories?
As shown on:
http://stackoverflow.com/questions/16665868/filter-posts-by-multiple-checkbox-categories-wordpressAre there any plugin available?
Thank you so much!
Best rgds,
VivienOctober 19, 2014 at 8:32 am #127974October 19, 2014 at 2:48 pm #128078Hi there,
Thanks for posting in.
That is possible by custom development.
Example, by adding this at your child theme’s functions.php
function x_filter_by_category( $query ) { if ( $query->is_home() && $query->is_main_query() && isset($_GET['cat_select']) ) { $query->set( 'cat', implode( ',', $_GET['cat_select'] ) ); } } add_action('pre_get_posts', 'x_filter_by_category');
Then somewhere on your template/or widget add this code. (let say you have wp-sidebar.php at your child theme’s /framework/views/{STACK}/ folder.
<?php if ( x_get_content_layout() != 'full-width' ) : ?> <!----- START : THIS IS FILTER FORM UNDER wp-sidebar.php --------> <form action="<?php echo home_url()?>"> <?php $categories = get_categories(); foreach ($categories as $category) { echo '<input type="checkbox" name="cat_select[]" value="'.$category->cat_ID.'"> '.$category->category_nicename.'<br />'; } ?> <button type="submit">Filter</button> </form> <!----- END : THIS IS FILTER FORM UNDER wp-sidebar.php --------> <aside class="<?php x_sidebar_class(); ?>" role="complementary"> <?php if ( get_option( 'ups_sidebars' ) != array() ) : ?> <?php dynamic_sidebar( apply_filters( 'ups_sidebar', 'sidebar-main' ) ); ?> <?php else : ?> <?php dynamic_sidebar( 'sidebar-main' ); ?> <?php endif; ?> </aside> <?php endif; ?>
Cheers!
-
AuthorPosts