我的代码在woocommerce中为我的产品添加了一个自定义字段:
add_action( 'woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3 );
function shoptimizer_custom_author_field() { ?>
<?php if(get_field('author')) { ?>
<div class="cg-author"><?php the_field('author'); ?></div>
<?php }
}
现在我想在if语句中添加一个条件:“如果字段不是空的,请隐藏产品标题”。
产品页产品标题的类似乎是"product_title“。
一旦它被添加到上面的代码中,它将是多么的吸引人。我认为这不是什么大事,但我的理解以HTML和CSS结束。
发布于 2022-10-24 01:52:32
我不确定我是否正确地理解了您的问题,但是如果您想隐藏产品标题(如果自定义字段不是空的),可以使用以下代码:
add_action('woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3);
function shoptimizer_custom_author_field() {
if (get_field('author')) {
echo '<style>.product_title { display: none; }</style>';
}
}
如果要隐藏自定义字段为空的产品标题,可以使用以下代码:
add_action('woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3);
function shoptimizer_custom_author_field() {
if ( ! get_field('author')) {
echo '<style>.product_title { display: none; }</style>';
}
}
发布于 2022-11-01 10:23:41
为了总结这条主线,下面任何一个寻求类似答案的人都可以复制和粘贴对我有用的解决方案:
add_action( 'wp_head', function() {
?><style>
h1.product_title.entry-title {
display: none;
}
.cg-title h1.product_title.entry-title {
display: block !important;
}
}</style><?php
} );
add_action( 'woocommerce_single_product_summary', 'shoptimizer_custom_author_field', 3 );
function shoptimizer_custom_author_field() { ?>
<?php if(get_field('author')) { ?>
<div class="cg-author"><h1><?php the_field('author'); ?></h1></div>
<?php }
else {?>
<div class="cg-title"><?php woocommerce_template_single_title(); ?> </div>
<?php
}
}
https://stackoverflow.com/questions/74179138
复制