B
B
Boris Belov2016-02-05 23:09:08
WordPress
Boris Belov, 2016-02-05 23:09:08

How to make a div not displayed if a custom field is empty?

Good evening.
The situation is what:
There is a code that displays a table. It has a line and two cells.
1st cell constant: icon + title
2nd cell variable: custom ACF field.

<tr class="tr01">
<td><img class="harimg" src="http://kosmosauto.com/wp-content/uploads/2016/02/har011.png" /> Привод:</td>	
<td><?echo get_post_meta($post->ID, 'har01', true);?></td>	
</tr>

It is logical to assume that if an arbitrary field is filled, then it looks like this:
04f0d1c1bcd24d749bbd5b2567df2be4.jpg
But if it is not filled, then it looks like this:
9339393e585e4a9db55ef74618917bd3.jpg
Hence the question: how to make it so that IF the cell is empty , THEN the line is hidden. If the cell is full, then everything is as usual. Help with this question please. Haven't found a solution yet. <?echo get_post_meta($post->ID, 'har01', true);?><tr class="tr01">

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-02-06
@iborisbelov

Everything is as WP_Panda says, only you can optimize it + if it's ACF, then it's better not through get_post_meta, but through get_field/the_field:

<?php if( ! empty( get_field( 'har01' ) ) ) : ?>
<tr class="tr01">
    <td><img class="harimg" src="http://kosmosauto.com/wp-content/uploads/2016/02/har011.png" /> Привод:</td>	
    <td><?php the_field( 'har01' ); ?></td>	
</tr>
<?php endif; ?>

Well, coding standards too :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question