显示/隐藏带标题的自定义 php 字段
Show / Hide custom php field with title
如果数据库字段有值,我想显示我们[使用 Estatik wordpress 房地产插件] 的自定义字段,如果它为空,则隐藏它。如果为空或已填写,我可以让该字段显示和隐藏,但是,当填写年份时,我无法显示文本:“Year built”:
<?php if( es_the_property_field('year-built1594411179f5f08c8ab6ff14') ): ?>
<p>Year built: <?php es_the_property_field('year-built1594411179f5f08c8ab6ff14'); ?></p>
<?php endif; ?>
我可以更改上面的代码来实现什么:
如果填写年份,我希望它显示:
建成年份:1994
如果年份留空,我希望该段落完全不打印
提前致谢!
对于 WordPress 中的大多数数据,有 the_[…]()
和 get_the_[…]()
样式函数。一般the_
函数会输出数据,get_the_
函数会return。大多数插件和主题具有相似的功能。我下载了演示版 Estatik 插件,发现它也具有该功能。
在你的 if
语句中,你实际上在那里输出了年份字段,它不是 return 一个真实值,所以它停在那里(否则它会显示 1994Year Built: 1994
).
returning 函数是 es_get_the_property_field( $field, $post_id = 0 );
因此,将其与一些清理结合起来(利用 es_the_property_field
的 before/after 参数,您最终会得到如下结果:
<?php if( es_get_the_property_field('year-built1594411179f5f08c8ab6ff14') ){
es_the_property_field('year-built1594411179f5f08c8ab6ff14', '<p>Year built: ', '</p>' );
} ?>
但是:
这是 es_the_property_field()
函数的完整标记:
/**
* Render property field.
*
* @param $field
* @param string $before
* @param string $after
*/
function es_the_property_field( $field, $before = '', $after = '' ) {
$result = es_get_the_property_field( $field );
echo ! empty( $result ) ? $before . $result . $after : null;
}
这意味着您实际上可以完全删除 if
语句,因为该函数会检查它对您来说是否为空。因此,您可以将其进一步简化为:
<?php es_the_property_field( 'year-built1594411179f5f08c8ab6ff14', '<p>Year Built: ', '</p>' ); ?>
Note: I don't have any personal experience with this plugin, this is all just based on what I found in the estatik/functions.php
file.
我不熟悉 Estatik 主题,但我可以告诉你,你可以像这样测试内容。
<?php
$year_built = es_property_field('year-built1594411179f5f08c8ab6ff14');
if( $year_built != "" || $year_built != false ): ?>
<p>Year built: <?php echo date("Y", strtotime ($year_built)); ?></p>
<?php endif; ?>
所以你遗漏的两件事是在 if 语句中进行适当的测试,然后用日期格式回显 year_built。现在这是假设一些事情。
- es_the_property_field 回显而不是返回,这将是一个非常 wordpress 的事情
- 从该函数中删除“the”是该函数的版本 returns 而不是 echos。
如果数据库字段有值,我想显示我们[使用 Estatik wordpress 房地产插件] 的自定义字段,如果它为空,则隐藏它。如果为空或已填写,我可以让该字段显示和隐藏,但是,当填写年份时,我无法显示文本:“Year built”:
<?php if( es_the_property_field('year-built1594411179f5f08c8ab6ff14') ): ?>
<p>Year built: <?php es_the_property_field('year-built1594411179f5f08c8ab6ff14'); ?></p>
<?php endif; ?>
我可以更改上面的代码来实现什么:
如果填写年份,我希望它显示: 建成年份:1994
如果年份留空,我希望该段落完全不打印
提前致谢!
对于 WordPress 中的大多数数据,有 the_[…]()
和 get_the_[…]()
样式函数。一般the_
函数会输出数据,get_the_
函数会return。大多数插件和主题具有相似的功能。我下载了演示版 Estatik 插件,发现它也具有该功能。
在你的 if
语句中,你实际上在那里输出了年份字段,它不是 return 一个真实值,所以它停在那里(否则它会显示 1994Year Built: 1994
).
returning 函数是 es_get_the_property_field( $field, $post_id = 0 );
因此,将其与一些清理结合起来(利用 es_the_property_field
的 before/after 参数,您最终会得到如下结果:
<?php if( es_get_the_property_field('year-built1594411179f5f08c8ab6ff14') ){
es_the_property_field('year-built1594411179f5f08c8ab6ff14', '<p>Year built: ', '</p>' );
} ?>
但是:
这是 es_the_property_field()
函数的完整标记:
/**
* Render property field.
*
* @param $field
* @param string $before
* @param string $after
*/
function es_the_property_field( $field, $before = '', $after = '' ) {
$result = es_get_the_property_field( $field );
echo ! empty( $result ) ? $before . $result . $after : null;
}
这意味着您实际上可以完全删除 if
语句,因为该函数会检查它对您来说是否为空。因此,您可以将其进一步简化为:
<?php es_the_property_field( 'year-built1594411179f5f08c8ab6ff14', '<p>Year Built: ', '</p>' ); ?>
Note: I don't have any personal experience with this plugin, this is all just based on what I found in the
estatik/functions.php
file.
我不熟悉 Estatik 主题,但我可以告诉你,你可以像这样测试内容。
<?php
$year_built = es_property_field('year-built1594411179f5f08c8ab6ff14');
if( $year_built != "" || $year_built != false ): ?>
<p>Year built: <?php echo date("Y", strtotime ($year_built)); ?></p>
<?php endif; ?>
所以你遗漏的两件事是在 if 语句中进行适当的测试,然后用日期格式回显 year_built。现在这是假设一些事情。
- es_the_property_field 回显而不是返回,这将是一个非常 wordpress 的事情
- 从该函数中删除“the”是该函数的版本 returns 而不是 echos。