简体   繁体   English

显示/隐藏带有标题的自定义 php 字段

[英]Show / Hide custom php field with title

I want to display a custom field we have [using the Estatik wordpress real estate plugin] if the database field has a value and hide that if it is empty.如果数据库字段有值,我想显示我们拥有的自定义字段[使用 Estatik wordpress 房地产插件],如果它是空的,则隐藏它。 I can get the field to display and hide if empty or filled out, but, I cannot display the text: "Year built" when the year is filled out:如果为空或已填写,我可以显示和隐藏该字段,但是,当填写年份时,我无法显示文本:“Year built”:

<?php if( es_the_property_field('year-built1594411179f5f08c8ab6ff14') ): ?>
    <p>Year built: <?php es_the_property_field('year-built1594411179f5f08c8ab6ff14'); ?></p>
<?php endif; ?>

What can i change the code above to achieve:我可以更改上面的代码来实现什么:

If the year is filled out, I'd like it to display: Year built: 1994如果填写年份,我希望它显示:建造年份:1994

If the year is left empty, i'd like the paragraph to not print at all如果年份为空,我希望该段落根本不打印

thanks in advance!提前致谢!

For most data in WordPress, there are the_[…]() and get_the_[…]() style functions.对于 WordPress 中的大多数数据,有the_[…]()get_the_[…]()样式函数。 Generally the the_ functions will output the data, and the get_the_ functions will return .通常the_函数会输出数据, get_the_函数会返回 Most plugins and themes have similar functionality.大多数插件和主题都具有类似的功能。 I downloaded the demo Estatik plugin and noticed that it does have that functionality as well.我下载了演示 Estatik 插件并注意到它也具有该功能。

In your if statement, you're actually outputting the year field there, and it's not returning a truthy value, so it stops there (otherwise it would show 1994Year Built: 1994 ).在你的if语句中,你实际上是在那里输出 year 字段,它没有返回一个真实的值,所以它停在那里(否则它会显示1994Year Built: 1994 )。

The returning function is es_get_the_property_field( $field, $post_id = 0 );返回函数是es_get_the_property_field( $field, $post_id = 0 );

So, coupling that with a little bit of cleanup (utilizing the before/after arguments of es_the_property_field , you would end up with something like this:因此,将其与一些清理相结合(利用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>' );
} ?>

HOWEVER:然而:

Here's the whole markup for the es_the_property_field() function:这是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;
}

This means you could actually drop your if statement altogether, because that function checks to see if it's empty for you.这意味着您实际上可以完全删除if语句,因为该函数会检查它是否为空。 So you could simplify it even further to just:因此,您可以进一步将其简化为:

<?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/functions.php文件中找到的estatik/functions.php

I am not familiar with the Estatik theme but I can tell you that you could test the content like this.我不熟悉 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; ?>

So the two things you were missing was a proper test in the if statement and then echoing out the year_built with the date format.因此,您缺少的两件事是 if 语句中的正确测试,然后使用日期格式回显 year_built。 Now this is assuming a few things.现在这是假设一些事情。

  1. that es_the_property_field echos instead of returning which would be a very wordpress thing to do es_the_property_field 回显而不是返回,这将是一个非常 wordpress 的事情
  2. that removing the "the" from that function is the version of that function that returns instead of echos.从该函数中删除“the”是该函数返回而不是回声的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM