PHP conditional statement to show a field - php

I have inserted a code to display product attribute in view.phptml, which works fine. But it shows a empty box, even when the field is empty.
The code I used to display is like this
<div class="add-to-box">
<div class="add-to-cart"><div>image path</div>
<h3><?php
echo $_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift')
?></h3></div></div>
How I can add a condition to above statement to hide the empty box from display when the field is empty.!

Use either empty or is_null, like so:
<?php
if( !empty( $_product->getFreeGift() ) ){
?>
... HTML here ...
<?php
}
?>
or
<?php
if( !is_null( $_product->getFreeGift() ) ){
?>
... HTML here ...
<?php
}
?>

Hi may be you can use this
<?php
if (isset($_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift'))) {
echo('<div class="add-to-box">
<div class="add-to-cart">
<div>image path</div>
<h3>'.$_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift').'</h3>
</div>
</div>');
} else {
..... // other stuff
}
The isset function here help to know if a variable contain something usefull.
good work !

Related

Wordpress: Advances Custom Field multiple select

I’ve been wracking my brain tonight trying to figure out how to display multiple labels from a select field.
First I tried it with a single field and that is working. Now I want to make use of multiple fields but can't get it working. Can someone help me?
Thanks!
<?php if ( get_field( 'locationCompany', $joboffer->ID) ) : ?>
<span class="company-compact"><? echo (get_field('locationCompany', $joboffer->ID))?></span>
<?php endif ?>
Make sure that you enable the Select multiple values? in the ACF settings. Here's your code:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<span class="company-compact"><?php echo $value; ?></span>
<?php
endforeach;
endif;
?>
You can use the same approach as above but you need to wrap the whole div with the foreach:
<?php
$locationcompany = get_field( 'locationCompany', $joboffer->ID);
if ( $locationcompany ) :
foreach ($locationcompany as $value): ?>
<div id="job-offer-filter"
class="col-xl-4 col-lg-6 col-md-6 col-sm-12 col-xs-12 height-130 job-offer-card all
<? echo (get_field('company', $joboffer->ID))?>
<? echo ($value) /*** HERE ***/?>
<? echo (get_field('workingtime', $joboffer->ID) === "fulltime" ? "Fulltime" : "Parttime")?>
<? echo (get_field('jobCategories', $joboffer->ID))?>">
<?php
endforeach;
endif;
?>
Please note if this div is in the same file with the span tag from above, you can just put this div inside the foreach. To avoid repetition of code.

Wordpress Advanced Custom Fields if statment

I can't seem to display only the content I request. I am using the Advanced Custom Fields (ACF) plugin with my Wordpress site, and using a repeater field with a multiple select field for "departments \ job titles".
With the code below, I am looking to display only the content which has the value that equals what I select. Maybe I am looking at this wrong but the content displays whether the strpos is true or not.
<?php if (get_field('policy_links') ): ?>
<center><h3><a name="All"></a>All Employees</h3></center>
<hr>
<ul class="bullets">
<?php while (has_sub_field('policy_links')) : ?>
<?php $jobtype = get_sub_field('who_is_this_for'); ?>
<?php if (strpos ($jobtype, 'ma') !== false) { ?>
<li><a href="<?php the_sub_field('link_to_the_document'); ?>">
<?php the_sub_field('display_name'); ?></a><span> - <small>
<?php the_sub_field('link_notes'); ?></small></span>
<?php the_sub_field('who_is_this_for'); ?></li>
<?php } else { ?><p>fail </p>
<?php }; // end of the loop. ?>
<?php endwhile; // end of the loop. ?>
</ul>
<?php endif; ?>
Your if statement should be done like this:
<?php if(get_sub_field('who_is_this_for') == 'strpos') { ?><?php }?>
Where "strpos" is the value you select (if I follow correctly).
It would probably be beneficial to actually see your fields.
I was on the right track. It turns out the ACF is converted to an array when multiple values are selected. I performed an implode to the variable then used the converted string performed the validation.
thanks for the help

How to hide a div is function doesn't exist?

I am adding a custom layout for Genesis that will place the blubrry player. I got it correctly but my problem is the wrapping div. It is visible if no Podcast is added.
This is the code for the player:
<?php if function_exists('the_powerpress_content') the_powerpress_content(); ?>
Here is my complete code:
add_action('genesis_entry_header', 'the_powerpress_player');
function the_powerpress_player(){
if( is_single() || in_category('5') ){
?>
<div class="podcast-div"><?php if function_exists('the_powerpress_content') the_powerpress_content(); ?></div>
<?php
}
else {
echo "<div class="no-podcast">";
echo "<div>";
}
}
Is there other way where the div wrapper will show only if if the podcast attachment is added? I tried the above code but I got critical error. With no 'else' clause the div is rendered but I wanted it invisible if no podcast in the post.
TIA
Replace
echo "<div class="no-podcast">";
With
echo "<div class='no-podcast'>";
-----------^----------^---
<?php function the_powerpress_player(){
if( is_single() || in_category('5') ){ ?>
<div class="podcast-div">
<?php if function_exists('the_powerpress_content') the_powerpress_content();?>
</div>
<?php } else { ?>
<div class="no-podcast" style="display:none;">
</div>
<?php }?>

How do I do If Post Meta DOES NOT exist?

I have the following code:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Later in my code I want to be able to say that if buy-link does not exist - i.e. there is no data in that field - then do something, else do something different.
Not sure how to do this! Help appreciated!
(By the way, I posted this question to Wordpress Stack Exchange first. It was voted closed there because it apparently concerns PHP boolean logic more than Wordpress - https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)
<?php if($buycheck ==''){ /*stuff*/ } ?>
this will render $buycheck, and if it is empty == is equal to '' nothing.
You can set a global variable that you can check later to see if the buylink exists:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
Then later on in your code:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
If you need the actual value, you can set a global containing the return value from get_post_meta so you can use the actual value.

How to hide "image" custom field if empty

I created an unordered list of custom fields and I wish to hide them if they are empty. For text custom fields I used the code:
<?php if (get_field('phone') != '') { ?>
<li><strong>Phone: </strong><?php the_field('phone'); ?></li>
<?php } ?>
However, I have a custom field which is for images, like this:
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
How can I hide the field if no image was uploaded (obviously, the above code won't work)?
Thanks in advance.
I think it should be
<?php if (get_field('logo') != ''): ?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php endif; ?>
Assuming the_field('logo') will return a falsy value if there are no images
if (the_field('logo')) {
?>
<li><strong>Logo: </strong><img src="<?php the_field('logo'); ?>"></img></li>
<?php
}
<?php if( get_field('field_name') ): ?>
<p>My field value: <?php the_field('field_name'); ?></p>

Categories