conditional statement if all is empty, don't show - php

<ul class="artist-links">
<?php if(!empty($artist_website)) { ?><li class="artist-website"><img src="<?php bloginfo('template_url') ?>/images/artist-website.png" alt="<?php the_title(); ?> Website" /></li><?php } ?>
<?php if(!empty($artist_youtube)) { ?><li class="artist-youtube"><img src="<?php bloginfo('template_url') ?>/images/artist-youtube.png" alt="<?php the_title(); ?> Youtube" /></li><?php } ?>
<?php if(!empty($artist_twitter)) { ?><li class="artist-twitter"><img src="<?php bloginfo('template_url') ?>/images/artist-twitter.png" alt="<?php the_title(); ?> Twitter" /></li><?php } ?>
</ul>
I want to make this unordered list show up only if at least one of the variables (artist-website, artist-youtube, artist-twitter) isn't empty. So if all of the variables are empty, then this list won't show up at all. What can I add before this code to make this work?

I would suggest changing your design a little bit.
//Initialize the variables and define a fixed array for easy manipulation
if($website) { $artist['website'] = $website; }
if($youtube) { $artist['youtube'] = $youtube; }
if($twitter) { $artist['twitter'] = $twitter; }
// If somehow, $artist contains values
if(count($artist)) { ?>
<ul class="artist-links"
<? foreach($artist as $key=>$value) { ?>
<li class="artist-<?=$key?>">
<a href="<?=$value?>" target="_blank">
<img src="theimage.jpg" />
</a>
</li>
<? } ?>
<?
}
?>

Just check at the beginning if they are all empty:
Code
<?php if(!(empty($artist_website) && empty($artist_youtube) && empty($artist_twitter))) : ?>
<ul class="artist-links">
<?php if(!empty($artist_website)) { ?><li class="artist-website"><img src="<?php bloginfo('template_url') ?>/images/artist-website.png" alt="<?php the_title(); ?> Website" /></li><?php } ?>
<?php if(!empty($artist_youtube)) { ?><li class="artist-youtube"><img src="<?php bloginfo('template_url') ?>/images/artist-youtube.png" alt="<?php the_title(); ?> Youtube" /></li><?php } ?>
<?php if(!empty($artist_twitter)) { ?><li class="artist-twitter"><img src="<?php bloginfo('template_url') ?>/images/artist-twitter.png" alt="<?php the_title(); ?> Twitter" /></li><?php } ?>
</ul>
<?php endif; ?>
Explanation
Basically we check each variable for the condition "empty". If all 3 are empty, the result of AND(true,true,true) is true. Then we see the "!" or the NOT operator, which reverses that true and makes it a false. So if all the variables are empty, then do no print the lines in between.
You may also notice the ":" / "endif" syntax i've used, which I find much neater when splicing into tags of html.

If you want to do exactly what you said above:
I want to make this unordered list
show up only if at least one of the
variables (artist-website,
artist-youtube, artist-twitter) isn't
empty.
Do this
<?php
if(!empty($artist_website) || !empty($artist_youtube) || !empty($artist_twitter)) {
//Do what you want to do. Like show the ordered list.
} //End of if statement
?>
If at least one of the variables has a non empty value the conditional is passed.

Related

Variable not being formatted with CSS - Magento

When I echo my own variable to a div container it comes out as plaint text (unformatted). I don't understand why echoing a Magento variable comes out formatted but mine doesn't? Here's my code, in particular the <?php if(!isset($specialPrice)): { ?> section which I created. Here is the code:
<div class="product">
<a href="<?php echo $_item->getProductUrl() ?>"
title="<?php echo $this->escapeHtml($_item->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail') ?>"
alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a>
<div class="product-details">
<p class="product-name">
<?php echo $this->escapeHtml($_item->getName()) ?>
</p>
<?php $specialPrice = $productToCheck->getData('special_price');
$orignalPrice = $productToCheck->getData('price');
?>
<?php if(!isset($specialPrice)): { ?>
<?php echo $product['price'] ?>
<?php } else: { ?>
<?php echo $specialPrice ?>
<?php } endif ?>
</div>
</div>
Echoing $product['price'] shows up with its CSS like this:
but if it enters the ELSE statement to display my variable it shows like this:
Does anyone know what could be going wrong?
$product['price'] returns you a unformatting price value.
Maybe you can call a block function, for example $block->getPrice() and in getPrice() you need to format the price by your custom preferences.
If you want to add styles to your price value then you need to use a magento tag class

PHP - if statement with multiple values

I have the following code:-
<ul class="grid effect-2" id="grid">
<?php
if( have_rows('gallery') ):
while ( have_rows('gallery') ) : the_row(); ?>
<?php
$image_location = get_sub_field('image_location');
if (empty($_GET['filter'])) {
$image_filter = $image_location == 'Nottingham' || $image_location == 'Corby';
} else {
$image_filter = $image_location == $_GET['filter'];
}
?>
<?php
if($image_filter) {
?>
<li><a class="fancybox" rel="gallery" href="<?php the_sub_field('image'); ?>" title="<?php echo $image_location . ' # Planet Bounce ' . get_sub_field('image_location'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php echo get_sub_field('image_caption') . ' # Planet Bounce ' . $image_location; ?>" /></a></li>
<?php } ?>
<?php endwhile;
else : endif;
?>
</ul>
$image_location can either be 'Nottingham' or 'Corby' or both. The image filter is basically filtering which images are being shown.
Although the above code works, I don't think it is right to do it this way.
If anybody could help me with a better practise way of doing the same query that would be much appreciated.
If you need me to explain in better detail, please let me know.
The condition can be wrapped in a two liner code with use of array.
$imageFilters = array('Nottingham'=>'Nottingham', 'Corby'=>'Corby');
$image_filter = isset($imageFilters[$_GET['filter']]) ? $imageFilters[$_GET['filter']] : $_GET['filter'];

PHP WordPress if else not working unexpected syntax

I have a PHP if else statement however it does not seem to work and I'm not sure why.
Here is my code
<?php
if (has_post_thumbnail()) {
?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php
the_post_thumbnail('full', array('class' => 'img-responsive alignleft'));
?>
</a>
<?php
} else () {
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php
}
?>
And here is the error that I get syntax error, unexpected ')'
I'm not sure where the unexpected ) is coming from.
I am basing my PHP on this structure, however editing it as I would like to be able to put mine into HTML without using echo
<?php if ( '' != get_the_post_thumbnail() ) {
// some code
}
else {
// some code
}
?>
Let's try with this:
<?php if (has_post_thumbnail()): ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail('full', array('class' => 'img-responsive alignleft')); ?>
</a>
<?php else: ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />';
<?php endif; ?>
I'm not sure why you wrote your code this way instead of concatenating it, but your if else statement has two parenthesis after else "()". You might want to get rid of these to fix your issue.
In PHP else is not a function, so you shouldn't call it like you did.
PHP's elseif () needs conditions,
try else instead:
<?php
if ( has_post_thumbnail() ) {
?>
<?php the_post_thumbnail( 'full', array( 'class' => 'img-responsive alignleft' ) ); ?>
<?php
} else { //instead of elseif () here, use else
?>
<img src="<?php echo get_template_directory_uri(); ?>/images/thumb.png" />
<?php
}
?>
Please also refer to the WordPress Coding Standards to make your code easier readable and maintainable. And try not to edit the original code in your question without marking the edits clearly, otherwise SO users won't be able to understand your problem/help you further.

Wordpress php else if statement with advanced custom fields

I'm trying to make an if-else-statement which works by going if there is a link print it around the name.
I have the below code which is almost there. However, the link is being printed above the text ranther than being an actual link.
<?php if( the_sub_field('corporate_link') ){ ?>
<a href="<?php the_sub_field('corporate_link'); ?>"
target="_blank"
title="<?php the_field('corporate_name'); ?>"><?php the_field('corporate_name'); ?></a>
<?php } else { ?>
<?php the_sub_field('corporate_name'); ?>
<?php } ?>
Any thoughts on how to make it link instead of printing the link if it`s there?
So what im looking to acheive is if there is a link print this
Coprate Name
If there isn't a link it just shows the corporate name.
use get_sub_field('corporate_link') instead of the_sub_field('corporate_link')
<?php
$corporate_link = get_sub_field('corporate_link');
$corporate_name = get_sub_field('corporate_name');
if( $corporate_link != '' ){ ?>
<a href="<?php echo $corporate_link; ?>"
target="_blank"
title="<?php echo $corporate_name; ?>"><?php echo $corporate_name; ?></a>
<?php } else { ?>
<?php echo $corporate_name; ?>
<?php } ?>
use get_sub_field() and get_field() instead of the_sub_field() and the_field()

Output image src with SimpleXML

I'm trying to output an image with SimpleXML, but the image tag doesn't appear in the source code.
Can anyone help me outpout this image:
Here's my XML and code:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
<h3><?php echo $event->title; ?></h3>
<p><strong><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></strong></p>
<p><?php echo $event->location; ?></p>
</a>
</li>
<?php } ?>
Your issue is here:
<?php if ($event->Media['url'] == !null) { ?>
<img src="<?php echo $event->Media['url'];?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
You're trying to access url as though it were an attribute, you need to access it as a child element by using ->url instead.
<?php if ($event->Media->url != null) { ?>
<img src="<?php echo $event->Media->url;?>" alt="<?php echo $event->title;?> thumbnail" />
<?php } ?>
EDIT: By the way, == !null works as you expect, but != null is a bit friendlier and less confusing
Your if statement is incorrect. It should be:
if ($event->Media['url'] != null)

Categories