如果另一个为空,则显示内容的IF语句php



我有一些字段(Wordpress(,有时其中一个字段是空的。当没有填写"short_description"时,我想显示"the_except"的内容。

这是我带来的:

if (empty(the_field('short_description'))) {
the_excerpt();
} else {
the_field('short_description'); 
}

不幸的是,它同时显示short_description和except。这里怎么了?我错过什么了吗?对我来说,代码看起来不错。

要检查值是否存在,请首先使用get_field()函数而不是the_field()

请看一个例子,它展示了如何在显示值之前检查是否存在。

<?php if( get_field('short_description') ): ?>
<?php the_field('short_description'); ?>
<?php else: ?>
<?php  the_excerpt(); ?>
<?php endif; ?>

或者你可以用另一种方式使用,比如:

$isValue = get_field( "short_description" );
if( $isValue ) {
echo $isValue ;
} else {
the_excerpt();
}

WooCommerce:清空时显示自定义简短描述

add_action( 'woocommerce_single_product_summary', 'bbloomer_echo_short_desc_if_empty', 
21 );
function bbloomer_echo_short_desc_if_empty() {
global $post;
if ( empty ( $post->post_excerpt  ) ) {
$post_excerpt = '<p class="default-short-desc">';
$post_excerpt .= 'This is the default, global, short description.<br>It will 
show if <b>no short description has been entered!</b>';
$post_excerpt .= '</p>';
echo $post_excerpt;
}
}

这是一个右

最新更新