关于php:如何基于is_page()条件标签在WordPress上为不同页面排入多个CSS和JS文件?

How to enqueue multiple css and js files on WordPress for different pages based on the is_page() conditional tag?

我是PHP / WordPress的新手,在这里我想做的是通过使用is_page()条件将不同的CSS和JS文件放入不同的页面。

尽管我认为这是此处广泛讨论的主题,但我仍然没有找到一种巧妙的方法来将多个文件(css / js)完全排入队列,然后将is_page()设置为有条件,以便可以将其中一些文件排入队列。每个"不同的页面基础"。

以下代码可以很好地实现我的主题,但是,我想知道是否有更好的方法来实现它。我真的很感谢有关此问题的一些指导。文本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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 ...我的意思是,以用户Jeffrey的身份优化代码后还建议,如果我要入队的页面超过一页,该怎么办,比如说:4页?我是否应该一直写" if(is_page('')){}"这种循环结构?


您实际上可以像这样制作脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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_scripts接受css和jquery入队。

干杯!