【问题标题】:Don't display a custom field in a list if the field is empty如果字段为空,则不要在列表中显示自定义字段
【发布时间】:2017-12-17 11:16:03
【问题描述】:

早上好。 我已经设置了一个自定义字段修改 function.php 的列表。一切正常,但我想在字段为空时隐藏。我已经阅读了很多关于它的帖子,但我无法摆脱我的问题。

这里是我的 php 代码:

   // Show advanced custom fields in product detail page
add_action( 'woocommerce_single_product_summary', "ACF_product_content", 26 );
 

function ACF_product_content(){

 
  if (function_exists('the_field')){
    echo '<b>MATERIA: </b>';
    the_field('materia'); 
echo "<br />";
    echo '<b>FECHA DE EDICIÓN: </b>';
    the_field('fecha_de_edicion');
echo "<br />";
echo '<b>LUGAR DE EDICIÓN: </b>';
    the_field('lugar_de_edicion');
echo "<br />";
echo '<b>ISBN: </b>';
    the_field('isbn');
echo "<br />";
echo '<b>ISBN DIGITAL: </b>';
    the_field('isbn_digital');
echo "<br />";
echo '<b>ENCUADERNACIÓN: </b>';
    the_field('encuadernacion');
echo "<br />";
echo '<b>INTERIOR: </b>';
    the_field('interior');
echo "<br />";
echo '<b>MEDIDAS: </b>';
    the_field('medidas');
echo "<br />";
echo '<b>NÚMERO DE PÁGINAS: </b>';
    the_field('numero_de_paginas');
echo "<br />";
echo '<b>IDIOMA: </b>';
    the_field('idioma');
echo "<br />";
echo '<b>CÓDIGOS IBIC: </b>';
    the_field('ibic');
  }
  
} 

如果其中一个字段为空,谁能帮我隐藏它? 谢谢

【问题讨论】:

  • 使用if 检查该字段是否为空,如果if 为假,则通过不打印来隐藏它。你可以用一个 for 循环和一个数组或其他东西来做这件事,但基本的 if 是一个开始。

标签: php wordpress field custom-fields


【解决方案1】:

改为使用get_field,然后使用条件检查该字段是否为空,然后使用echo该字段。

例子:

$materia = get_field('materia');
if($materia) {
    echo '<b>MATERIA: </b>';
    echo $materia;
}

【讨论】:

  • 非常感谢。答案是完美的
  • @Stefano 好。不要忘记接受答案。 :)
猜你喜欢
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多