Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wordpress中添加除管理员之外的每个页面的Javascript_Javascript_Wordpress - Fatal编程技术网

在wordpress中添加除管理员之外的每个页面的Javascript

在wordpress中添加除管理员之外的每个页面的Javascript,javascript,wordpress,Javascript,Wordpress,正在寻找有关通过functions.php或Wordpress中其他任何地方插入此代码的帮助。我试过这个,但似乎不起作用。。。我在源代码中看不到它 <?php function add_this_script_footer(){ ?> <script type="text/javascript"> var _paq = _paq || []; _paq.push(["trackPageView"]); _paq.p

正在寻找有关通过functions.php或Wordpress中其他任何地方插入此代码的帮助。我试过这个,但似乎不起作用。。。我在源代码中看不到它

<?php
 function add_this_script_footer(){ ?>
    <script type="text/javascript">
        var _paq = _paq || [];
        _paq.push(["trackPageView"]);
        _paq.push(["enableLinkTracking"]);


        (function() {
        var u=(("https:" == document.location.protocol) ? "https" : "http") + "://stats.domain.com/statsapp/";
        _paq.push(["setTrackerUrl", u+"piwik.php"]);
        _paq.push(["setSiteId", "445"]);
        var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
        g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
        })();
    </script>    <?php }

add_action('wp_footer', 'add_this_script_footer'); ?>

变量| paq=| paq |[];
_paq.push([“trackPageView”]);
_paq.push([“enableLinkTracking”]);
(功能(){
var u=((“https:==document.location.protocol)?“https:“http”)+“:///stats.domain.com/statsapp/”;
_push([“settrackerrl”,u+“piwik.php]”);
_paq.push([“设置站点ID”,“445”]);
var d=document,g=d.createElement(“脚本”),s=d.getElementsByTagName(“脚本”)[0];g.type=“text/javascript”;
g、 defer=true;g.async=true;g.src=u+“piwik.js”;s.parentNode.insertBefore(g,s);
})();
谢谢你的帮助


`

您应该将其添加到模板文件中-要么添加到header.php,要么添加到footer.php

正确的方法是使用wp\u enqueue\u脚本,并将代码放入自己的js文件中

因此,获取跟踪代码并将其保存在类似site-tracking.js的文件中

然后在functions.php文件中执行以下操作:

add_action('wp_enqueue_scripts', 'my_custom_scripts');

function my_custom_scripts(){
  /** here you can apply whatever logic you want to determine which pages
   *  your script gets included on, like wrap this in is_singular()
   */
  wp_enqueue_script('tracking', get_template_directory_uri().'/js/site-tracking.js', array(), '', null, true);

}

通过这种方式,您的javascript可以正确排队,您可以在一个单独的、自包含的文件中进行更新,并根据需要进行注册和注销。

最灵活的方法是使用该函数在子主题函数文件中使用自定义函数并自定义参数

依赖于jQuery

add_action( 'wp_enqueue_scripts', 'child_enqueue_scripts' );

function child_enqueue_scripts() {

if ( ! is_admin() ) {

wp_register_script( 'script-handle', get_bloginfo( 'stylesheet_directory' ) . '/your.js', array( 'jquery' ), '1.0.0', true );

wp_enqueue_script( 'script-handle' );

    }
}
完成后,您可以在文件中的其他代码中使用脚本句柄,而无需再次注册

如果希望将脚本放置在结束标记之前,请将第5个参数$in_footer设置为true,以便在页脚中执行。 默认值:false

此方法使您能够将WordPress和特定于主题的挂钩与条件标记一起使用,这比在模板文件中添加脚本更灵活

子主题与父主题

改变

get_bloginfo( 'stylesheet_directory' )

get_template_directory_uri()
在父主题中添加脚本时

无依赖关系

在您的子主题函数文件中使用下面的示例,用于不依赖jQuery的没有依赖项的脚本(如跟踪脚本)

add_action( 'wp_enqueue_scripts', 'no_dependencies_enqueue_scripts' );

function no_dependencies_enqueue_scripts() {

if ( ! is_admin() ) {

wp_register_script( 'script-handle', get_stylesheet_directory_uri() . '/your.js', false, '1.0.0', true );

wp_enqueue_script( 'script-handle' );

    }

}

注意:两个代码示例都假定包含脚本的文件位于子主题的根目录中。如果将脚本文件添加到单独的文件夹中,请更改此路径。

属于:尝试此操作后,无效。。我将它添加到theme compat文件夹中的footer.php中,这是正确的文件夹吗?它必须是/wp content/themes/(活动主题的名称)/footer.php或header.php,但如果您将它放在footer.php中,它应该就在关闭标记之前。需要明确的是,您只想添加部分,而不是php代码。我在哪里添加此代码,wp includes文件夹中的functions.php?我可以使用它作为根目录的路径:get_bloginfo(url)。/piwik-script.js将其添加到主题的functions.php文件中。在本例中,主题文件夹中有一个名为js的文件夹。您使用的路径意味着您的js文件位于WP的根文件夹中。你想在wp content/themes/your theme/jsI的wp content>Treature,twentyeleven和twentyten中有3个文件夹我怎么知道它是哪一个,我会为几个网站做这个,有没有办法不用登录每个网站的管理员就知道?在浏览器中查看源代码——你的样式表链接会显示哪个文件夹。比如:
add_action( 'wp_enqueue_scripts', 'no_dependencies_enqueue_scripts' );

function no_dependencies_enqueue_scripts() {

if ( ! is_admin() ) {

wp_register_script( 'script-handle', get_stylesheet_directory_uri() . '/your.js', false, '1.0.0', true );

wp_enqueue_script( 'script-handle' );

    }

}