我编写了以下代码,第二个嵌套的IF语句被忽略了。如果我改变它们的顺序,就会发生同样的事情。我已经尝试了一个elseif语句,但似乎也没有用。
任何想法为什么?我猜这是一个很漂亮的菜鸟问题,所以请欺骗我:)
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '169969516705075');
fbq('track', "PageView");
<?php
if($_GET['order_total'] && $_GET['product_id']) {
$order_total = $_GET['order_total'];
$product_id = $_GET['product_id'];
if($product_id === 8421 || 1925 || 1932) {
$output = "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
echo $output;
}
if($product_id === 1647) {
$free_output = "fbq('track', 'CompleteRegistration');";
echo $free_output;
}
}
if (is_cart()) {
$output = "fbq('track', 'AddToCart');";
echo $output;
}
if(is_checkout()) {
$output = "fbq('track', 'InitiateCheckout');";
echo $output;
}
if (is_wc_endpoint_url( 'order-received' )) {
$output = "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
echo $output;
}
?></script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
答案 0 :(得分:6)
其他答案解决了您的核心问题 - 这就是PHP没有按照您的想法执行此声明:
if ($product_id == 8421 || 1925 || 1932) {...
这总是评价为正,因为你说:
if $product_id == 8421 (which does a comparison as intended)
OR
1925 (which is not a comparison, but rather is a non-zero integer, which is "truthy")
OR
1932 (which is another non-zero integer, which is also "truthy")
then...
所以,作为编码风格的问题,只要我检查的是多个值,我就喜欢使用in_array
:
if ( in_array( $product_id, array( 1925, 1932, 8421 ) ) {...
但是,您还有更多事情要做,我也想帮助您。
WordPress最佳做法不会将您的代码放在模板文件的顶部,而是放在函数文件中。
因此,请使用相同的代码,但改为将其更改为主题的functions.php
文件。
请注意,它将基于wp_head钩子(我已经包含在下面)来获得CALLED,这样可以保护模板文件不受逻辑/功能影响:
// First, hook into the "wp_head" so that this is output in the head
add_action('wp_head','facebook_output');
<?php
// This function is called by the 'wp_head' action above
function facebook_output() {
// Close the PHP tag to output some script simply
?>
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '169969516705075');
fbq('track', "PageView");
<?php // Re-open the PHP tag so we can do our PHP logic
if($_GET['order_total'] && $_GET['product_id']) {
$order_total = $_GET['order_total'];
$product_id = $_GET['product_id'];
// For maintainability and readability, put the array(s) of values into variables
$products_purchase = array(8421, 1925, 1932);
$products_registration = array(1647);
// Check if the product_id is in the array
if( in_array( $product_id, $products_purchase ) ) {
// No need to assign to $output and then echo - just echo
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
if( in_array( $product_id, $products_registration ) ) {
echo "fbq('track', 'CompleteRegistration');";
}
}
if ( is_cart() ) {
echo "fbq('track', 'AddToCart');";
}
if( is_checkout() ) {
echo "fbq('track', 'InitiateCheckout');";
}
if ( is_wc_endpoint_url( 'order-received' ) ) {
echo "fbq('track', 'Purchase', {value: '$order_total', currency: 'USD'});";
}
// Close the PHP tag again to output the straight script
?>
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=169969516705075&ev=PageView&noscript=1"
/></noscript>
<!-- End Facebook Pixel Code -->
<?php // And finally re-open the PHP tag
}
答案 1 :(得分:1)
if($product_id === 8421 || $product_id ===1925 || $product_id ===1932)
是这样做的方法