I have a custom post type and archive page. I want to be able to use the default twentyfourteen theme slider so that the archive-cpt.php page calls the js and displays a slider only for the CPT. I imagine I call a function, but I don't know how to write the script or how to call it from. I know the js location of the slider. Any help here would be really appreciated.
1 Answer
The slider script is called with the following lines of code
if ( is_front_page() && 'slider' == get_theme_mod( 'featured_content_layout' ) ) {
wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
'prevText' => __( 'Previous', 'twentyfourteen' ),
'nextText' => __( 'Next', 'twentyfourteen' )
) );
}
You just need to copy these lines and add them in your child theme and change your condition accordingly. The following in your child theme should work. Note: I believe here that you leave the js files in the parent theme
add_action( 'wp_enqueue_scripts', 'enqueue_slider_scripts', 11 );
function enqueue_slider_scripts()
{
if ( is_post_type_archive( 'cpt' ) // Change 'cpt' your match your exact cpt
&& 'slider' == get_theme_mod( 'featured_content_layout' )
) {
wp_enqueue_script( 'twentyfourteen-slider', get_template_directory_uri() . '/js/slider.js', array( 'jquery' ), '20131205', true );
wp_localize_script( 'twentyfourteen-slider', 'featuredSliderDefaults', array(
'prevText' => __( 'Previous', 'twentyfourteen' ),
'nextText' => __( 'Next', 'twentyfourteen' )
) );
}
}
EDIT
Just remember to copy the following lines to your archive page to display your slider
<?php
if ( twentyfourteen_has_featured_posts() )
get_template_part( 'featured-content' );
?>
-
Cheers Pieter, I assume this code goes into the function.php file?– OllieCommented Jul 13, 2015 at 23:08
-
Yes, that would be correct for the first section. The second block of code goes into your archive page Commented Jul 14, 2015 at 3:47
-
I did what you tried, and instead of starting the slider it posts what would be the slider, it piled posts on top of each other, and didn't call from the CPT posts. Instead, it called from the main blogroll.– OllieCommented Jul 14, 2015 at 5:01
-
If you need posts only from the specific post type, you will need to customize the slider. Commented Jul 14, 2015 at 5:04
-