Displaying a slider in your category pages

By default, the slider can be displayed before posts or pages in the Customizr theme but you might want to display a slider for a specific category (or for all).

The following snippet allows you to do so in two simple steps

1 First, create a slider and grab its name (see how to create a slider in Customizr )

2Then add one of the snippet below in your functions.php file.

 

Example 1 : displaying one slider for all categories

In the example below, just replace MY-SLIDER-NAME by your slider name.

add_filter( 'tc_show_slider' , 'show_slider_in_categories' );
function show_slider_in_categories( $bool ) {
  return is_category() ? true : $bool ;
}
 
add_filter( 'tc_slider_name_id', 'force_slider_name');
function force_slider_name( $original_slider_name ) {
  return is_category() ? 'MY-SLIDER-NAME' : $original_slider_name ;
}
 
add_filter( 'tc_slider_active_status' , 'force_active_slider' );
function force_active_slider($bool) {
  return is_category() ? true : $bool ;
}
//display in full width
add_filter( 'tc_slider_layout' , 'force_full_width' );
function force_full_width($bool) {
  return is_category() ? true : $bool ; /* replace true with false if you want to display the boxed slider */
}
//allow autoplay
add_filter( 'tc_customizr_script_params', 'allow_autoplay');
function allow_autoplay( $_params ) {
  $_params['SliderName'] = 'MY-SLIDER-NAME';
  return $_params;  
}

 

Example 2 : displaying a different slider by categories

This snippet is a bit longer. It basically assigns a slider name for each category for which you want to display a slider.

Just replace the example values in the $category_list array with yours and you are done.

 

Important : the $category_list uses the category slugs (and not the names). For example, if you have been created a category named My Awesome Category, you will have to use my-awesome-category (which is the slug) in the $category_list array.

More about category slug in WordPress.

 

function tc_get_slider_name_by_cat( $category_slug = null ) {
  //Associates category slugs to slider :  'slug' => 'slider name'
  $category_list = array (
        'category-one'      => 'slider-one',
        'category-two'      => 'slider-two',
        'category-three'    => 'slider-three',
  );
  return ( ! is_null($category_slug) ) ? $category_list[$category_slug] : $category_list;
}
 
function tc_get_cat_slug() {
  global $wp_query;
  $cat_slug = $wp_query -> query_vars;
  return isset($cat_slug['category_name']) ? $cat_slug['category_name'] : false;
}
 
 
add_filter( 'tc_show_slider' , 'tc_show_slider_in_categories' );
function tc_show_slider_in_categories( $bool ) {
  return ( is_category() && array_key_exists( tc_get_cat_slug(), tc_get_slider_name_by_cat() ) ) ? true : $bool ;
}
 
 
add_filter( 'tc_slider_name_id', 'tc_force_slider_name');
function tc_force_slider_name( $original_slider_name ) {
  return ( is_category() && array_key_exists( tc_get_cat_slug(), tc_get_slider_name_by_cat() ) ) ? tc_get_slider_name_by_cat( tc_get_cat_slug() ) : $original_slider_name ;
}
 
add_filter( 'tc_slider_active_status' , 'tc_force_active_slider' );
function tc_force_active_slider($bool) {
  return ( is_category() && array_key_exists( tc_get_cat_slug(), tc_get_slider_name_by_cat() ) ) ? true : $bool ;
}


//display in full width
add_filter( 'tc_slider_layout' , 'tc_force_full_width' );
function tc_force_full_width( $original_layout_value ){
  $layout_value = 0; // 1 for full size slider, 0 for boxed;
  return ( is_category() && array_key_exists( tc_get_cat_slug(), tc_get_slider_name_by_cat() ) )  ?  $layout_value : $original_layout_value;
}


//allow autoplay
add_filter( 'tc_customizr_script_params', 'tc_allow_autoplay');
function tc_allow_autoplay( $_params ) {
  $_params['SliderName'] = ( is_category() && array_key_exists( tc_get_cat_slug(), tc_get_slider_name_by_cat() ) ) ? 'true' : $_params['SliderName'];
  return $_params;  
}

 

Where to paste this code? => in your functions.php file. I strongly recommend to create a child theme. Download a start-up child theme here.

Everything you need to know about child theme with Customizr here.

27 thoughts on “Displaying a slider in your category pages”

Comments are closed.