首页 > 解决方案 > How to load external .js to the body of a Wordpress Page?

问题描述

I would like to add this script to a PAGE on wordpress theme. I don't want it at header or footer, i want it in the body of the specific page i created in Wordpress.

This code works if it was in a normal html page on a folder where Wordpress is not installed. However, if i put it in Wordpress page, the .js file doesn't seems to start up.

I know there are ways to add .js to wordpress header and footer, but i want it in the body of a page. How do i do that?

<script src="<?php bloginfo('template_directory'); ?>/js/googleformstyler.js"

        form="https://docs.google.com/forms/d/e/1FAIpQLSeJZ5Es893O4ckhi8-aTOZQe4js9_q2DO1I4pBHT_ENXIpyXg/viewform?embedded=true">
</script>

标签: javascriptwordpress

解决方案


为什么你不想在页眉/页脚中加载它?下面的摘录来自下面提供的链接。使用以下代码对脚本进行排队是 wordpress 的首选方法。

您可以使用wp_enqueue_script().

wp_enqueue_script( 
    $handle, 
    $src = '',
    $deps = array(), 
    $ver = false, 
    $in_footer = false );

参考: https ://developer.wordpress.org/reference/functions/wp_enqueue_script/

如果尚未包含脚本并且所有依赖项都已注册,则根据脚本依赖项在正确的时间将脚本文件链接到生成的页面。您可以将脚本与先前使用该wp_register_script()函数注册的句柄链接,或者为该函数提供链接脚本所需的所有参数。 这是将 JavaScript 链接到 WordPress 生成页面的推荐方法。

这个答案有点通用,因为如果没有来自该 .js 文件的更多信息/代码,很难知道您要实现的目标。请随时更新您的问题,在此处发表评论,社区可能会进一步帮助您进行查询。


推荐阅读