Php 如何根据is_page()条件标记将不同页面的多个css和js文件排列在Wordpress上?
我对PHP/WordPress非常陌生,我在这里尝试的是使用条件语句将不同的css和js文件排列在不同的页面上 虽然我相信这是一个在这里被广泛讨论的话题,但我仍然没有找到一种简单的方法来将多个文件(css/js)一起排队,然后设置is_page()条件,以便这些文件中的一些可以按“不同的页面”排队 下面的代码在我的主题上运行良好,但是,我想知道是否有更好的方法来完成它。我真的很想得到一些关于这个问题的指导。TxtPhp 如何根据is_page()条件标记将不同页面的多个css和js文件排列在Wordpress上?,php,wordpress,Php,Wordpress,我对PHP/WordPress非常陌生,我在这里尝试的是使用条件语句将不同的css和js文件排列在不同的页面上 虽然我相信这是一个在这里被广泛讨论的话题,但我仍然没有找到一种简单的方法来将多个文件(css/js)一起排队,然后设置is_page()条件,以便这些文件中的一些可以按“不同的页面”排队 下面的代码在我的主题上运行良好,但是,我想知道是否有更好的方法来完成它。我真的很想得到一些关于这个问题的指导。Txt // To register my css styles I use the fu
// To register my css styles I use the function below:
function enqueue_extra_styles()
{
wp_register_style( 'custom-style', get_stylesheet_directory_uri() . '/css/custom-style.css', array(), '1', 'all' );
wp_register_style( 'second-custom-style', get_stylesheet_directory_uri() . '/css/second-custom-style.css', array(), '1', 'all' );
wp_register_style( 'third-custom-style', get_stylesheet_directory_uri() . '/css/niceforms/third-custom-style.css', array(), '1', 'all' );
if ( is_page('8')) {
//bellow styles will be enqueued only on a page of id=8
wp_enqueue_style('custom-style');
wp_enqueue_style( 'second-custom-style' );
}
//bellow style will be enqueued only on a page of id=2111
if ( is_page('2111')){
wp_enqueue_style('third-custom-style');
}
}
add_action('wp_enqueue_scripts', 'enqueue_extra_styles');
// To register my js files I use the function below:
function my_extra_jscripts()
{
wp_register_script('custom-script', get_template_directory_uri().'/js/custom-script.js', array('jquery'), '', TRUE);
wp_register_script('second-custom-script', get_template_directory_uri().'/js/second-custom-script.js', array('jquery'), '', TRUE);
if ( is_page('8')){
wp_enqueue_script('custom-script');
}
if ( is_page('2111')){
wp_enqueue_script('second-custom-script');
}
}
add_action('wp_enqueue_scripts', 'my_extra_jscripts');
正如其他用户所建议的,有几种方法可以优化上面的代码。我已经看到了这一点,并且我发现它在优化方面非常有希望,但是,我想在编写php“if conditional”时实现一种更好的方法……我的意思是,在按照用户Jeffrey的建议优化代码之后,如果我有超过1个页面要排队等待文件,那么接下来该怎么做,比如:4个页面?我应该一直写“if(is_page(“”)){}”这种循环结构吗?你可以这样编写你的脚本:
<?php
function custom_scripts_method() {
wp_register_style( 'custom-style', get_stylesheet_directory_uri() . '/css/custom-style.css', array(), '1', 'all' );
wp_register_style( 'second-custom-style', get_stylesheet_directory_uri() . '/css/second-custom-style.css', array(), '1', 'all' );
wp_register_style( 'third-custom-style', get_stylesheet_directory_uri() . '/css/niceforms/third-custom-style.css', array(), '1', 'all' );
wp_register_script('custom-script', get_template_directory_uri().'/js/custom-script.js', array('jquery'), '', TRUE);
wp_register_script('second-custom-script', get_template_directory_uri().'/js/second-custom-script.js', array('jquery'), '', TRUE);
if ( is_page('8')) {
wp_enqueue_style('custom-style');
wp_enqueue_style( 'second-custom-style' );
wp_enqueue_script('custom-script');
}
}
add_action( 'wp_enqueue_scripts', 'custom_scripts_method' );
?>
wp_enqueue_脚本同时接受css和jquery enqueue
干杯