0

我正在开发一个自定义主题,出于某种特定原因,我没有使用 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>

4

1 回答 1

0

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

有一个钩子叫wp_print_footer_scripts

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

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>
于 2021-07-24T12:54:09.407 回答