is_page
is working fine as it detects the slug. However, is_page_template
function isn't and so I tried global $template
too but that's not working either. How can I resolve this issue?
I want to load the the JS file for the page template-parts/content.php
function software_enqueue() {
if ( is_page( 'software' ) ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
global $template;
if ( basename( $template ) === 'template-parts/content.php' ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
}
add_action( 'wp_enqueue_scripts', 'software_enqueue' );
btw, I tried calling the file (fullwidth.php) too using the code below but that didn't work:
if ( is_page_template('fullwidth.php') ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
fullwidth.php
<?php
/**
* Template Name: Full Width
*/
get_header(); ?>
<section id="primary" class="content-area content-fullwidth">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();
is_page
is working fine as it detects the slug. However, is_page_template
function isn't and so I tried global $template
too but that's not working either. How can I resolve this issue?
I want to load the the JS file for the page template-parts/content.php
function software_enqueue() {
if ( is_page( 'software' ) ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
global $template;
if ( basename( $template ) === 'template-parts/content.php' ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
}
add_action( 'wp_enqueue_scripts', 'software_enqueue' );
btw, I tried calling the file (fullwidth.php) too using the code below but that didn't work:
if ( is_page_template('fullwidth.php') ) {
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
}
fullwidth.php
<?php
/**
* Template Name: Full Width
*/
get_header(); ?>
<section id="primary" class="content-area content-fullwidth">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
</section><!-- #primary -->
<?php
get_footer();
Share
Improve this question
asked Nov 22, 2019 at 0:50
Elaine ByeneElaine Byene
1177 bronze badges
4
|
1 Answer
Reset to default 1Kaperto is correct, you can enqueue in the actual page template file you want the javascript to appear in. If it would only need to be in a single template file (rather than every page that is fullwidth), I would create a new one which includes the JS, and use that as the template for the page. Ie:
<?php
/**
* Template Name: Sticky Kit Enabled
*/
wp_enqueue_script('sticky-kit.min.js', get_template_directory_uri().'/inc/assets/js/sticky-kit.min.js', false ,'1.0', 'all' );
get_header(); ?>
<section id="primary" class="content-area content-fullwidth">
<main id="main" class="site-main" role="main">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main><!-- #main -->
</section><!-- #primary -->
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744980776a4604428.html
get_header
. – Kaperto Commented Nov 22, 2019 at 0:55is_page_template('fullwidth.php')
is correct, and should work. The only reason it wouldn't is iffullwidth.php
was in a sub-folder, in which case it should be something liketemplates/fullwidth.php
, or if the template wasn't assigned to the page via Page Attributes > Template when editing the page, in which case you'll need to explain how you're actually using this template. – Jacob Peattie Commented Nov 22, 2019 at 4:42