WordPress自定义字段上的语法错误'<'



我们在网站商店部分的产品页面上添加了一个自定义购买按钮,用于我通过alpha和beta函数的可选字段定义为目录的一系列产品。

现在的问题是,这个按钮显示在所有产品中,即使是那些简单定义的产品,在实践中,该系列产品会显示两个购买按钮,一个是添加到WooCommerce自己的购物车中的按钮,另一个是我们放置的购买按钮。我们给

注意这个代码

add_action( 'woocommerce_after_single_product_summary', function()
{
global $post;
if( get_post_meta($post->ID, 'alpha', true) != '' ) {
$url = returnalphaUrl($post->ID);
}
elseif ( get_post_meta($post->ID, 'beta', true) != '' ) {
$url = returnbetaUrl($post->ID);
}
else {
$url = returnUrl();
}

?>
<button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
Buy Now
</button>

我们把购买按钮的显示放在最后,这就是为什么会出现这个问题

现在,当我们想在条件中显示按钮时,会出现语法错误"<">

add_action( 'woocommerce_after_single_product_summary', function()
{
global $post;
if( get_post_meta($post->ID, 'alpha', true) != '' ) {
$url = returnalphaUrl($post->ID);
<button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
Buy Now
</button>
}
elseif ( get_post_meta($post->ID, 'beta', true) != '' ) {
$url = returnbetaUrl($post->ID);
<button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">
Buy Now
</button>
}
else {
$url = returnUrl();
}

?>

你认为问题出在哪里?

以下是更正后的代码:

add_action( 'woocommerce_after_single_product_summary', function(){
global $post;
if( get_post_meta($post->ID, 'alpha', true) != '' ) {
$url = returnalphaUrl($post->ID); ?>
<button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">Buy Now</button>
<?php } else if( get_post_meta($post->ID, 'beta', true) != '' ) {
$url = returnbetaUrl($post->ID); ?>
<button id="Btn" onclick="window.location.href='<?php echo $url; ?>'">Buy Now</button>
<?php } else {
$url = returnUrl();
}
});
?>

你也可以这样回显按钮:

add_action( 'woocommerce_after_single_product_summary', function(){
global $post;
if( get_post_meta($post->ID, 'alpha', true) != '' ) {
$url = returnalphaUrl($post->ID); 
echo '<button id="Btn" onclick="window.location.href="'.$url.'">Buy Now</button>';
} else if( get_post_meta($post->ID, 'beta', true) != '' ) {
$url = returnbetaUrl($post->ID);
echo '<button id="Btn" onclick="window.location.href="'.$url.'">Buy Now</button>';
} else {
$url = returnUrl();
}
});
?>

相关内容

最新更新