【问题标题】:How can I show WP enqueued script or style in a custom section of the code? (Aside from the header and footer)如何在代码的自定义部分中显示 WP 排队脚本或样式? (除了页眉和页脚)
【发布时间】:2021-07-23 17:28:00
【问题描述】:

我正在开发一个自定义主题,出于某种特定原因,我没有使用 get_header() 函数。

我分别使用 wp_enqueue_script 和 wp_enqueue_style 将脚本和样式排入队列,并且如果我调用 get_header() 函数就可以让它工作。没问题。

问题是我正在自定义我的标题,而 get_header() 添加了我不需要的代码。如何让排队的脚本和样式显示在 get_header() 或 get_footer() 出现的页眉或页脚之外的地方?

例如:

<head>
    <title>This is the title</title>
    <!-- scripts
     ================ -->
     SHOW ENQUEUED SCRIPTS HERE

    <!-- styles
     ================ -->
     SHOW ENQUEUED STYLES HERE
</head>

【问题讨论】:

    标签: wordpress wp-enqueue-scripts


    【解决方案1】:

    我能够弄清楚,所以我会发布答案,因为我在 Google 中没有看到任何相关内容。

    有一个钩子叫做wp_print_footer_scripts

    如果您添加以下代码行,您可以让您的脚本和样式显示在您编写 sn-p 的位置:

    do_action( 'wp_print_footer_scripts', '_wp_footer_scripts' );
    

    如果要从脚本中拆分样式,可以使用_wp_footer_scripts回调函数中使用的两个函数:

    print_late_styles();
    
    print_footer_scripts();
    

    所以,问题中显示的示例代码最终会是这样的:

    <head>
        <title>This is the title</title>
        <!-- scripts
         ================ -->
         <?php print_footer_scripts(); ?>
    
        <!-- styles
         ================ -->
         <?php print_late_styles(); ?>
    </head>
    

    或者只是使用钩子:

    <head>
        <title>This is the title</title>
        <!-- scripts and styles
         ================ -->
        <?php do_action( 'wp_print_footer_scripts', '_wp_footer_scripts' ); ?>
    </head>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多