我试图仅在wordpress网站上从一个页面删除statcounter脚本(我有页脚),因此它不会计算该页面,特别是因为它是隐藏元素。
我查看了插件php,但它没有注册或排队任何脚本,所以我不知道使用什么功能。下面是它的一个片段:
$sc_position = get_option(key_sc_position);
if ($sc_position=="header") {
add_action('wp_head', 'add_statcounter');
} else {
add_action('wp_footer', 'add_statcounter');
}
// The guts of the StatCounter script
function add_statcounter() {
global $user_level;
$sc_project = get_option(key_sc_project);
$sc_security = get_option(key_sc_security);
$sc_invisible = 0;
$sc_invisible = get_option('sc_invisible');
if (
( $sc_project > 0 )
) {
?>
<!-- Start of StatCounter Code -->
<script>
<!--
var sc_project=<?php echo $sc_project; ?>;
var sc_security="<?php echo $sc_security; ?>";
<?php
if($sc_invisible==1) {
echo " var sc_invisible=1;\n";
}
define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN));
$protocol = defined('HTTPS') ? "https:" : "http:";
?>
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
//-->
document.write("<sc"+"ript src='" +scJsHost +"statcounter.com/counter/counter.js'></"+"script>");
</script>
<!-- End of StatCounter Code -->
<?php
}
}
我尝试过使用:
function no_stat_counter_scripts() {
if(!is_page(XXX)){
global $user_level;
remove_action( 'wp_head', array($user_level, 'add_statcounter') );
remove_action( 'wp_footer', array($user_level, 'add_statcounter') );
}
}
add_action( 'wp_footer', 'no_stat_counter_scripts' );
add_action( 'wp_head', 'no_stat_counter_scripts');
但它不起作用,任何人都知道如何才能完成它?
答案 0 :(得分:0)
所以你看到了:
$sc_position = get_option(key_sc_position);
if ($sc_position=="header") {
add_action('wp_head', 'add_statcounter');
} else {
add_action('wp_footer', 'add_statcounter');
}
如果是某个页面,我们不想这样做......
为了获得某个页面,请检查
How to get the current page name in WordPress?
if ($pagename != "thepageyoudontwanttohavestatson")
{
if ($sc_position=="header") {
add_action('wp_head', 'add_statcounter');
} else {
add_action('wp_footer', 'add_statcounter');
}
}
意思是,如果我当前的页面名称不等于我想忽略的页面,那么添加计数器......并删除你所做的代码
add_action( 'wp_footer', 'no_stat_counter_scripts' );
add_action( 'wp_head', 'no_stat_counter_scripts');
答案 1 :(得分:0)
<强>尝试:强>
<?php
function add_statcounter() {
if(is_page('id or slug of page to exclude')) return;
global $user_level;
$sc_project = get_option(key_sc_project);
$sc_security = get_option(key_sc_security);
$sc_invisible = 0;
$sc_invisible = get_option('sc_invisible');
if ( ( $sc_project > 0 )) {
?>
<!-- Start of StatCounter Code -->
<script>
<!--
var sc_project=<?php echo $sc_project; ?>;
var sc_security="<?php echo $sc_security; ?>";
<?php
if($sc_invisible==1) {
echo "var sc_invisible=1;\n";
}
define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN));
$protocol = defined('HTTPS') ? "https:" : "http:";
?>
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
//-->
document.write("<sc"+"ript src='" +scJsHost +"statcounter.com/counter/counter.js'></"+"script>");
</script>
<!-- End of StatCounter Code -->
<?php
}
}