WP Custom Fields, hide code if field is empty? - php

I'm using a custom field template plugin for WP. I want to hide
<li>Sqft: [squareft]</li>
if the field is empty. I've tried different codes, these are two I've tried based on suggestions:
<?php if ('squareft' !== '') { ?><li>Sqft: [squareft]</li>
<?php } ?>
And
<?PHP $squareft = ('squareft'); if ($squareft != '') { echo '<li>Sqft: [squareft]
</li>';} if (empty($squareft)) { echo " "; } ?>
I obviously don't have a clue what I'm doing, although I'm learning through trial and error. It uses shortcodes, so [squareft] is what to use to output the field data.
Any help is appreciated.
Update:
I think I've got it working, based on doing this method. Not yet gone live but it's working in my test post.
<?php
global $post;
$bathrooms = get_post_meta($post->ID, 'bathrooms', true);
if ( !empty($bathrooms) ) { echo '<li>Baths: [bathrooms]</li> | ' ; }
?>

Try this (replacement of the if statement in your first code sample):
<?php if (do_shortcode('[squareft]') != '') { ?>
<li>Sqft: [squareft]</li>
<?php } ?>
Or if you know the custom meta field's actual field name (generated by the plugin, probably some kind of prefix + squareft) you can do:
<?php if (get_post_meta($post->ID, 'squareft', true) != '') { ?>
It would help to know the specific plugin you are using.

Using the get custom fields plugin I have used the following to hide content if the field it empty (in this case the 'info' field):
<?php $info = c2c_get_custom('Info');?>
<?php if ( $info == "" ) {echo "";} else {echo "<h2 id=comments>Notes</h2><div id=info>$info</div>" ;} ?>
Not sure if this will help but it might give someone else more ideas.
Are tring this :
<?php if ( (c2c_get_custom('image')) ) { ?>
blah blah blah php code etc...
<?php } ?>
Works perfectly. If field has something it shows, if not no show, it was leaving a broken image when empty, now its good.

<?php if (get_post_meta($post->ID, 'squareft', true) != '') { echo "display output";?>

Related

If subtitle present issue

I'm using the newest wordpress.
In my theme, I'm using the following code:
<?php
if (the_subtitle() == "") {
echo the_title();
} else {
echo the_subtitle();
} ?>
Each page has a default title. Some pages have a subtitle. If there IS a subtitle, then the title shouldn't be displayed and the subtitle should take it's place.
But right now for some reason, it's displaying the subtitle and THEN the title?
You don't need to call echo. It already echoes.
<?php
if (the_subtitle("","", false) == "") {
the_title();
} else {
the_subtitle();
} ?>

PHP - Logical operator with IF/Else statement test not working correctly

Code from: oc-content/themes/realestate/item.php
I have this piece of PHP code which is part of a 'framework' called OSClass. Basically, I am trying to 'hide' the user (the publisher of the advert) name to non-logged in users. In other words, only if a user is logged in, only then they can see if the publisher users advert.
I found out that I need to add this piece of code osc_is_web_user_logged_in(), which has worked perfectly for another section, however, due to the else statement, the publishers name is still displaying...
How can I amend the else statement? I could remove the else statement, but I'm worried it will 'break' something and I am unsure what osc_item_user_id() does... Do I add another if statement within the else statement (complete PHP newbie here).
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } else { ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
Thanks!
In my opinion you should simple write:
<div class="ico-author ico"></div>
<?php if( osc_item_user_id() != null && osc_is_web_user_logged_in() ){ ?>
<?php echo osc_item_contact_name(); ?>
<?php } ?>
As you don't want to display publisher name when user is not logged, you don't need have else section.
You can add an else if statement.
if (osc_item_user_id() != null && osc_is_web_user_logged_in())
{
<?php echo osc_item_contact_name(); ?>
}
else if (user non logged)
{
// your stuff
}
else
{
<?php echo osc_item_contact_name(); ?>
}

2 pieces of similar PHP, one works one doesn't?

I've got a few fields on a property site, grabbing a value in English & depending on the value, translating it (if another language other than English is selected).
This piece of code works fine:
<?php if(get_post_meta($post->ID,'prop_parking',true) && $prop_parking):
$prop_parking_meta = get_post_meta($post->ID,'prop_parking',true);
if ($prop_parking_meta == 'Yes') {
$prop_parking_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_parking_meta == 'No') {
$prop_parking_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_PARK_CSTM;?>:</p><p> <?php _e( $prop_parking_meta ); ?></p>
</li>
<?php endif; ?>
I get back Yesin the set language, yet in this field I don't (I just see Yes or No):
<?php if(get_post_meta($post->ID,'prop_garage',true) && $prop_garage):
$prop_garage_meta = get_post_meta($post->ID,'prop_garage',true);
if ($prop_garage_meta == 'Yes') {
$prop_garage_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_garage_meta == 'No') {
$prop_garage_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_GARG_CSTM;?>:</p><p> <?php _e( $prop_garage_meta ); ?></p>
</li>
<?php endif; ?>
Is it something obvious I'm missing? :( Thanks!
I don't know why this issue happens sometimes in qTranslate, but there are two options to deal with it:
using the shortcode notation
$prop_garage_meta = '[:en]Yes[:es]Sí[:ru]да';
applying the_content filter
$prop_garage_meta = apply_filters(
'the_content',
'<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->'
);

Elseif in WordPress with three outputs (third one doesn't show up)

Can't get this to work, been trying different things - but it only shows the two first. The last one (the else) doesn't show up on the website.
Can anyone see what I'm doing wrong with this one?
Thank you! :)
<?php if ( get_post_meta($post->ID, 'ends', true) ) { ?>
<?php echo get_post_meta($post->ID, 'ends', true); ?>
<?php } elseif ( shortcode_exists( 'postexpirator' ) ) { ?>
<?php echo do_shortcode('[postexpirator]'); ?>
<?php } else { ?>
Continues
<?php } ?>
I think it's getting hung up on:
<?php } elseif ( shortcode_exists( 'postexpirator' ) ) { ?>
And checking if the shortcode exists in general? I'm not too sure how you have this setup because it's a shortcode but I think that is probably what it is.

Custom fields multiple values, Wordpress, PHP

I am new to this with php, but working to learn, and i can't figure this out.
Here goes the original code with one value i get working:
$description = get_post_meta($post->ID, "description", false);
if ($description[0]=="") {
<!-- If there are no custom fields, show nothing -->
} else {
<div class="wrap">
<h3>Products</h3>
foreach($description as $description) {
echo '<p>'.$description.'</p>';
}
</div>
}
I have difficulties to modify this code to retrive values from more then one custom field input and putting it out together and inbetween the same .
Here is code with one more value added, but im uncearten how to combine thoose 2 values:
$description = get_post_meta($post->ID, "Description", false);
$seccond_value = get_post_meta($post->ID, "price", false);
<?php
if ($description[0]=="") {
?>
<strong> how do i combine 2 values here?</strong>
<!-- If there are no custom fields, show nothing -->
<?php
} else {
?>
<div class="wrap">
<h3>products</h3>
<strong>// how does this part work and how to i combine 2 values here?</strong>
<?php
foreach($description as $description) {
echo '<p>'.$description.'</p>';
}
?>
</div>
}
Maybe someone here has a good tip for a good sajt that covers my questions above?
Try this
if ($description[0] != "" && $seccond_value[0] != "") {
// Combine two values and print them
} else {
// Value is not set
}

Categories