我正在尝试仅在wordpress网站上的一个页面上删除statcounter脚本(我在页脚中),因此它不会计算该页面,特别是因为它的隐藏元素。
我已经研究了插件 php,但它没有注册或排队任何脚本,所以我不知道要使用什么函数。下面是其 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');
但它不起作用,有人知道我如何完成它吗?
所以你看到这个:
$sc_position = get_option(key_sc_position);
if ($sc_position=="header") {
add_action('wp_head', 'add_statcounter');
} else {
add_action('wp_footer', 'add_statcounter');
}
如果它是某个页面,我们不想这样做......
为了获得某个页面,请查看此内容
如何在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');
尝试:
<?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
}
}