Reputation: 77
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. 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:
<?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
If the year is left empty, i'd like the paragraph to not print at all
thanks in advance!
Upvotes: 0
Views: 366
Reputation: 370
I am not familiar with the Estatik theme but I can tell you that you could test the content like this.
<?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. Now this is assuming a few things.
Upvotes: 0
Reputation: 13860
For most data in WordPress, there are the_[…]()
and get_the_[…]()
style functions. Generally the the_
functions will output the data, and the get_the_
functions will return. Most plugins and themes have similar functionality. I downloaded the demo Estatik plugin and noticed that it does have that functionality as well.
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
).
The returning function is 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:
<?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:
/**
* 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. 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.
Upvotes: 1