从functions.php调用javascript

时间:2019-05-30 09:38:45

标签: php wordpress

我有一个javascript,看起来像这样:

jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering] li").click(function(){
    var selectedlevering = jQuery("ul[data-attribute_name=attribute_pa_afhentning-og-levering]").find("li.selected").attr("data-value");
    if (selectedlevering == "afhentning"){
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning] span").click();
        jQuery("ul[data-attribute_name=attribute_pa_packages] li[data-value=afhentning]").hide(); // hide the "afhentning" option in "Packages" if you like
    }
});

我需要从Wordpress中的functions.php调用该脚本。但是我该怎么做?

最诚挚的问候

1 个答案:

答案 0 :(得分:1)

您可以通过将wp_head添加为必需的操作来选择在顶部添加自定义脚本。

//----------------------------------------------------------------------
// Custom script in head section, add these lines in your functions.php
//----------------------------------------------------------------------

function custom_script_name(){
?>
<script>
// Your script here
</script>
<?php
}
add_action('wp_head', 'custom_script_name');