I'm using the following section of code to display some icons on our Magento store with the idea being if there is nothing added in the icons section it shouldn't display, for some reason this isn't working...it is displaying a division as if something is there but there is actually nothing.
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons') !== null):
?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->geticons()), 'icons') ?>
</div>
<?php endif; ?>
It needs to show Icons if they are coded in the attribute field and then hide the division if there is nothing added.
I've worked out that the code is returning a value of string(0) what do I need to change in my coding to get the desired effect?
Here's the thing you need, and you don't need call the same functionality twice to get the empty result. Define your variable and check if it is empty (null, undefined or false) or not
<?php $icons = $_helper->productAttribute($_product,($_product->getIcons()), 'icons');?>
<?php if(!empty($icons)):?>
<div class="product-icons">
<?php echo $icons;?>
</div>
<?php endif;?>
this could be even better solution as it wont call the helper unless there are icons defined but you first have to try it out on your codebase.
<?php if($_product->getIcons()):?>
<div class="product-icons">
<?php echo $_helper->productAttribute($_product,($_product->getIcons()), 'icons') ?>
</div>
<?php endif; ?>
and please check if it isn't a typo and it really is:
$_product->geticons()
or should it be
$_product->getIcons()
Something like:
<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons'))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>
would be better!
try
<?php
if(!empty($_helper->productAttribute($_product,($_product->geticons()), 'icons')))
{
echo "<div class=\"product-icons\">";
echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
echo "</div>";
}
?>
Related
Hi I have used some code but I want it so when the name is NULL it is hidden, if this is possible please help.
Here is the code
<p id="top"><strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]<br/>Welcome To the homepage of The Journalist's</p>
I want it to be when name is NULL, for
"<strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]"
to be hidden.
Thank you.
update...
This is what i have so far
This is what i have so far.
<?php
if ($SESSION['name']= NULL) {
?>
<div><strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]</div>
<?php
}
?>
I have fixed it , thank you for everyone that has commented. for anyone else with this problem. here is my solution.
<?php
if ($_SESSION['name']!= NULL) {
?>
<strong>Welcome back <?php echo $_SESSION['name']; ?> [Logout]<br/>
<?php
}
?>
Update from your posted code:
For one, you are using $SESSION instead of $_SESSION. Second, you are setting $SESSION['name'] using a single equal sign, so the if condition would always fail because it is evaluating if (NULL). You need to use == or ===. Read up on comparison operators.
<?php
if (condition) {
?>
<div></div>
<?php
}
?>
I am using this library http://wptheming.com/options-framework-theme/ for creating a theme option page in my admin panel in wordpress.
I want to know How can I check the variable that if the value is inserted on the input box or not
right now I am printing the value like this
<?php echo of_get_option('title', '');?>
I want to do like this
if(isset($title)){
<?php echo of_get_option('title', '');?>
}
Thanks
You can try this:
<?php if (of_get_option('title')) {?>
<?php echo $title ?>
<?php } else { ?>
<?php echo "No Title Submitted in Options Framework" ?>
<?php } ?>
I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.
I have a WordPress site in which I have added a few extra fields for Author contact fields. Some people are going to use Google plus but some will not. I don't want to have an empty paragraph tag etc. I can't seem to figure out how to write an if statement that works. This is what I was working with. Thanks for your time.
<?php if(get_the_author_meta('google')) ?> { ?>
<p><?php the_author_meta('google'); ?></p>
<?php }
else {
// do nothing
} endif; ?>
<?php if(get_the_author_meta('google')): ?>
<p><?php the_author_meta('google'); ?></p>
<?php
else:
// do nothing
endif; ?>
Did you try this? Also, your code has the opening brace for 'if' without a php tag.
try this - and put a class of some sort on there to ensure it's not showing up in the spots where it's not filled in - a border or something?
<?php if (get_the_author_meta('google')) { ?>
<p class="error"><?php the_author_meta('google') ?></p>
<?php }
else {
// do nothing
} ?>
This seems to work. Maybe it's the endif ?
I'm having trouble writing an if else statement. Considering that I've been doing so for years in ColdFusion, this make me feel very stupid.
Here's the task.
I need to pull first name, last name, email, co-chair status from a database and return the results. However not everyone has an email, so I need a statement that will include a mailto link for those that have emails, and exclude a mailto link for those that don't.
Here's the code I'm using that includes the mailto link for all.
What adjustments do I need to make?
thanks,
david
<?php do { ?>
<?php echo $row_GetMembers['BACmember_First']; ?> <?php echo $row_GetMembers['BACmember_Last']; ?>
<?php /*START_PHP_SIRFCIT*/ if ($row_GetMembers['BACmember_CoChair']=="Yes"){ ?>
<strong> Co-Chair</strong>
<?php } /*END_PHP_SIRFCIT*/ ?><br />
<?php } while ($row_GetMembers = mysql_fetch_assoc($GetMembers)); ?>
This is the line of code you want to optionally display as a link (split for readability):
<a href="mailto:<?php echo $row_GetMembers['BACmember_Email']; ?>">
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
You'll want something like this instead:
<?php if (!empty($row_GetMembers['BACmember_Email'])): ?>
<a href='mailto:<?php echo $row_GetMembers['BACmember_Email']?>>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
</a>
<?php else: ?>
<?php echo $row_GetMembers['BACmember_First']; ?>
<?php echo $row_GetMembers['BACmember_Last']; ?>
<?php endif; ?>
If the email field isn't empty (the ! negates the result, so we're checking if it isn't empty), it prints the first and last name inside an anchor tag. If it is empty, it prints the name without it.
A more elegant solution may be to provide the email address as a separate field or link, as opposed to having a list of some names that are links and some that aren't.