我为我的子主题创建了一个插件,其中存储了自定义函数。此外,在插件中,我还加入了样式表和.js,它们控制两个模板的输出,用于定制的post类型,名为工作坊。(站点上唯一需要排队样式和.js的地方。)
考虑到这一点,当活动模板为单-workshop.php. the和存档-workshop.php. the时,我想调用以下.js和样式表:
下面使用的代码不适用于适当的if语句,而是在非条件状态下工作:
function renchlist_enqueue_styles() {
if ( is_page_template( array( 'archive-workshop.php', 'single-workshop.php' ) ) ) {
wp_enqueue_style('bootstrap-grid-nk', get_stylesheet_directory_uri() .'/library/bootstrap/css/bootstrap-grid.min.css');
wp_enqueue_style ( 'nk-owl-style', get_stylesheet_directory_uri () . '/library/owl/css/owl.carousel.css', array (), '1.3.3' );
wp_enqueue_style ( 'owl-style', get_stylesheet_directory_uri () . '/library/owl/css/owl.theme.default.min.css');
wp_enqueue_style('nk-chocolat-style', get_stylesheet_directory_uri() .'/library/chocolat/css/chocolat.css');
}
}
add_action('wp_enqueue_scripts', 'renchlist_enqueue_styles');
function renchlist_add_scripts() {
if ( is_page_template( array( 'archive-workshop.php', 'single-workshop.php' ) ) ) {
wp_enqueue_script( 'nk-combined-js', get_stylesheet_directory_uri() . '/library/renchlist-combined.js', array('jquery'), null, true );
wp_enqueue_script( 'google-map', 'https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXX', array(), '3', true );
wp_enqueue_script( 'google-map-init', get_stylesheet_directory_uri() . '/library/js/google_maps.js', array('google-map'), '0.1', true );
}我被认为是我错误的根源--感谢你的帮助。
发布于 2021-03-17 16:04:17
尝试使用is_post_type_archive()和is_singular():
// ...
if ( is_post_type_archive( 'my-post-type' ) || is_singular( 'my-post-type' ) ) {
// your enqueue code goes here
}
// ...这些函数将检查是否分别位于post类型的my-post-type或单个my-post-type post的存档页面上。(当然,在注册my-post-type时,将其更改为您命名的自定义post类型。)
https://wordpress.stackexchange.com/questions/385234
复制相似问题