有没有更简单的方法在 Wordpress 中为多个页面模板排队脚本
Is there a simpler way of enqueuing scripts for multiple page templates in Wordpress?
除了我的Wordpress主题中的标准page.php
之外,我还有一个名为full-page.php
的附加页面模板。我想将我不想在默认模板中排队的其他脚本 ( script-4.js
full-page.php
排队。目前,我已经使用以下方法将它们排队,使用if
和else
语句 - 与我的样式表排队的方式相同。
我想知道是否有办法在不将所有内容排队两次的情况下做到这一点?例如,在 full-page.php
中,我是否可以只排队script-4.js
,同时让它自动从默认模板中获取脚本 1、2 和 3?
function enqueue_my_styles_and_scripts() {
if (is_page_template('page-templates/full-page.php')) {
wp_enqueue_script(
'mytheme-script-1',
get_template_directory_uri() . '/js/script-1.js' );
wp_enqueue_script(
'mytheme-script-2',
get_template_directory_uri() . '/js/script-2.js' );
wp_enqueue_script(
'mytheme-script-3',
get_template_directory_uri() . '/js/script-3.js' );
wp_enqueue_script(
'mytheme-script-4',
get_template_directory_uri() . '/js/script-4.js' );
}
else {
wp_enqueue_script(
'mytheme-script-1',
get_template_directory_uri() . '/js/script-1.js' );
wp_enqueue_script(
'mytheme-script-2',
get_template_directory_uri() . '/js/script-2.js' );
wp_enqueue_script(
'mytheme-script-3',
get_template_directory_uri() . '/js/script-3.js' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_styles_and_scripts' );
你的意思是这样的:
function enqueue_my_styles_and_scripts() {
wp_enqueue_script(
'mytheme-script-1',
get_template_directory_uri() . '/js/script-1.js' );
wp_enqueue_script(
'mytheme-script-2',
get_template_directory_uri() . '/js/script-2.js' );
wp_enqueue_script(
'mytheme-script-3',
get_template_directory_uri() . '/js/script-3.js' );
if (is_page_template('page-templates/full-page.php')) {
wp_enqueue_script(
'mytheme-script-4',
get_template_directory_uri() . '/js/script-4.js' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_styles_and_scripts' );
相关文章:
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 借助asp.net验证或java脚本对多个文本进行验证
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- Java脚本时间添加
- 不显示带有本地json文件数据的谷歌地图脚本
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 从远程脚本获取用户IP
- 如何根据时间运行不同的脚本
- 如何将字符串值从php页面发送到java脚本页面
- 有没有更简单的方法在 Wordpress 中为多个页面模板排队脚本
- jQuery没有在Wordpress中定义,但我的脚本被正确排队
- 根据页面条件对脚本进行排队
- 将脚本与 PHP 决策排队
- WordPress-排队脚本/CSS的问题
- Wordpress排队脚本
- 如何从CDN将WordPress中的脚本排队
- 注册/排队脚本
- 在wordpress子主题中排队脚本顺序错误
- 如何停止在wordpress短代码内多次排队相同的脚本
- 是否可以在function.php wordpress上对脚本src进行排队