I have a restaurant menu theme and there are some menu items the owner has, with no pricing (all menu items have the option for a price and an alternate price)

In the taxonomy-menutype.php file [ shows the menu items ] there is this:

<div class="pricebox">
                        <?php if ( get_post_meta($post->ID,'atp_price',TRUE) ) {
                            echo'<span class="price">'.get_post_meta($post->ID,'atp_price',TRUE).'</span>';
                        } ?>

                        <?php 
                        $moreprices = get_post_meta(get_the_id(),'atp_moreprice',true);
                        if( is_array( $moreprices ) ){ ?>
                            <?php foreach( $moreprices as $itemprice ) {?>
                                <span class="price"><?php echo $itemprice; ?></span>
                            <?php } ?>
                        <?php } ?>
                        </div>

What I need to do is hide the (either) field if there is no input (empty) otherwise if there is, then show the field...

It's not sure if the OP adds that content with an Advanced Custom Field input in WP-admin.

The OP could also use CSS for this, but this requires that the element has no nothing in it. Not even a blank space.

.price:empty { display: none }

https://css-tricks.com/almanac/selectors/e/empty/

Oh my... had no idea about the CSS empty attribute, worked great..... Added it to the arsenal, thank you! :0 :)

Member Avatar for diafol

:empty is indeed an useful trick. Be aware that it does have one issue:

IE9-IE11 supports :empty but will not repaint/relayout the page if content is added/removed from an :empty selected element

Taken from http://caniuse.com/#feat=css-sel3

If you're not using javascript to dynamically add / remove items, then I doubt this should matter.

I must admit, I'm not a big fan of hiding stuff if they're not supposed to be on the page at all - hiding/showing as a toggle, fair enough, but adding the kitchen sink from the server and then getting css to clear up the mess is a bit worrying. Personally, I'd go with a server-side solution.

What is happenening is, the area for the price has a background color and padding set to it - but... when there is no price applied the field still shows on the page as a small rectangle of the background color.

All I desired to to do was hide it if there is no input (PRICE) entered by the admin, thats all. I tried some functions I found but they didnt work.

Member Avatar for diafol

Ah I see. I thought you wanted to hide the entire item. OK css should be good for that. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.