PHP - if statement with multiple values - php

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'];

Related

Changing Logo based on Page (Wordpress, PHP)

I'm trying to change the header logo on my site, depending on the page the person is on. I don't know PHP, but I've found where the Logo is defined in header.php, and am trying to rewrite it to be dynamic. When I use my code, the site breaks, so obviously I'm doing something wrong.
The original code is:
<!-- Logo -->
<?php
// Get the logo
if ($ti_option['site_logo'] != '') {
$site_logo = $ti_option['site_logo'];
}
else {
$site_logo = get_template_directory_uri() . '/images/logo.png';
}
?>
<a class="logo" href="<?php echo home_url('/'); ?>">
<img src="<?php echo $site_logo; ?>" alt="<?php bloginfo('name'); ?>
-
<?php bloginfo('description'); ?>" title="<?php bloginfo('name'); ?>
-
<?php bloginfo('description'); ?>" />
</a>
<!-- End Logo -->
What I'm trying to do is display a different logo based on which page the visitor is on. There are three:
- if the page is one of these: (1168, 1433, 1428), display /path/logo1.jpg
- if the page is one of these: (1369, 1361, 1365), display /path/logo2.jpg
- otherwise, just show /path/logo3.jpg
Here's what I've been able to manage so far:
<!-- Logo -->
<?php
// Get the logo
< ? php
if ($ti_option['site_logo'] != '') {
if (is_page(array(
1168,
1433,
1428
))) :
// '$site_logo' => 'FILELOCATION.jpg',
$site_logo = $ti_option['site_logo'];));
elseif (is_page(array(
1369,
1361,
1365
))):
$site_logo = $ti_option['site_logo'];));
else:
$site_logo = $ti_option['site_logo'];
endif;
?>
} else {
$site_logo = get_template_directory_uri() . '/images/logo.png';
}
?>
<a class="logo" href="<?php echo home_url('/'); ?>">
<img src="<?php echo $site_logo; ?>" alt="<?php bloginfo('name'); ?>
-
<?php bloginfo('description'); ?>" title="<?php bloginfo('name'); ?>
-
<?php bloginfo('description'); ?>" />
</a>
<!-- End Logo -->
I don't think I can nest PHP opening and closing tags, so there's that. But, I feel like I need to... my code doesn't work. Can anyone point me in direction of what to try?
I'm not 100% sure on wordpress standards, but here's a way to do it.
$logo1 = "logo1.jpg";
$logo2 = "logo2.jpg";
if ($ti_option['site_logo'] != '') {
if (is_page(array( 1168, 1433, 1428))){
$site_logo = $logo1;
}else if (is_page(array( 1369, 1361, 1365))){
$site_logo = $logo2;
}else{
$site_logo = $ti_option['site_logo'];
}
?>
} else {
I see 2 issues with your code :
1- No matter the outcome, you end up doing this : $site_logo = $ti_option['site_logo'];
In other words, you never actually change the logo.
2- Might be due to the fact you're using the dots if(cond): instead of if(cond){} which I'm not used to work with but imo it's much clearer when you have a good indentation.
error that is probably making your code crash :
$site_logo = $ti_option['site_logo'];));
should be
$site_logo = $ti_option['site_logo'];

Optimising php code for list of items

I'm using joomla! to output some extra fields into an article. The fields are a list of images (max of 10) which are displayed using backslider jquery plugin.
Here is the code i've used which works:
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
$img1 = $this->item->extrafields['image_1'];
$img2 = $this->item->extrafields['image_2'];
$img3 = $this->item->extrafields['image_3'];
$img4 = $this->item->extrafields['image_4'];
$img5 = $this->item->extrafields['image_5'];
$img6 = $this->item->extrafields['image_6'];
$img7 = $this->item->extrafields['image_7'];
$img8 = $this->item->extrafields['image_8'];
$img9 = $this->item->extrafields['image_9'];
$img10 = $this->item->extrafields['image_10'];
?>
<?php if($img1) { ?>
<li><img src="<?php echo $img1; ?>"></li>
<?php } ?>
<?php if($img2) { ?>
<li><img src="<?php echo $img2; ?>"></li>
<?php } ?>
<?php if($img3) { ?>
<li><img src="<?php echo $img3; ?>"></li>
<?php } ?>
<?php if($img4) { ?>
<li><img src="<?php echo $img4; ?>"></li>
<?php } ?>
<?php if($img5) { ?>
<li><img src="<?php echo $img5; ?>"></li>
<?php } ?>
<?php if($img6) { ?>
<li><img src="<?php echo $img6; ?>"></li>
<?php } ?>
<?php if($img7) { ?>
<li><img src="<?php echo $img7; ?>"></li>
<?php } ?>
<?php if($img8) { ?>
<li><img src="<?php echo $img8; ?>"></li>
<?php } ?>
<?php if($img9) { ?>
<li><img src="<?php echo $img9; ?>"></li>
<?php } ?>
<?php if($img10) { ?>
<li><img src="<?php echo $img10; ?>"></li>
<?php } ?>
</ul>
</div>
I'm not a php expert but is there a better way of optimising this code, I'm thinking maybe putting the $img variables into an array and using a foreach loop to output each list item?
A little help wouldn't go a miss :)
Just iterate over them and use the index to reference the array indices:
for ($i = 1; $i <= 10; ++$i) {
if (!empty($this->item->extrafields["image_$i"])) {
echo '<li><img src="', htmlspecialchars($this->item->extrafields["image_$i"]), '"></li>';
}
}
Assuming there are up to 10 items to investigate.
The solution below will avoid evaluating strings, cache's everything you can, to keep your application running smoothly, and keeps your code nice and tidy.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
// Generate length of our image array / store known length of array
$images = 10;
// Loop through images
for( $i = 1; $i <= $images; $i++ ) {
// Store it for optimization sake.
$field = $this->item->extrafields['image_' . $i];
// Check it's not empty
if( !empty( $field ) ) {
// If not, print to browser
printf(
'<li><img src="%s" alt=""></li>',
htmlspecialchars($field)
);
}
}
?>
</ul>
</div>
I think this code will do same thing.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
for($i = 1; $i <= 10; $i++)
{
$img = $this->item->extrafields['image_'.$i]
if($img) { ?>
<li><img src="<?php echo $img; ?>"></li>
<?php }
}
?>
</ul>
</div>

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)

conditional statement if all is empty, don't show

<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.

flickr phpflickr api

Overview
I am trying to get a photo feed on to my site using Flickr's api and the phpflickr library. I can successfully get the photoset on to my site, but it shows all the photos from every photoset, what I was hoping to achieve was to show the primary photo from each photoset, and then if the user clicked on the image it would show the full photoset in a lightbox/shadowbox.
My Code
<div id="images" class="tabnav">
<ul class="items">
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<li>
<a rel="shadowbox['<?=$count;?>']" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<p><?=$ph_set['description'];?></p>
</a>
</li>
<?php endforeach; ?>
<?php $count++; ?>
<?php endforeach; ?>
</ul>
</div>
Another Attempt
I have also tried calling the getPhotos function differently, instead of sending it without any parameters I sent it with parameters
$photos = $f->photosets_getPhotos($photoset_id, NULL, NULL, 1, NULL);
The above code stopped the showing all the photos from each photoset and started showing just the primary image, but it also stopped making the rest of the photos accesible to me.
Is there something I can do to make this work? I am totally out iof ideas.
Regards and thanks
I came up with this soltion, thought I would post it in case anyone else hits this problem,
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<li>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<?php if($parentID == $ph_set['parent']): ?>
<a rel="lightbox[album<?=$count;?>]" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<?php endif;?>
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<?php if($ph_set['description'] != null) :?>
<p><?=$ph_set['description'];?></p>
<?php endif; ?>
<?php if($parentID == $ph_set['parent']): ?>
</a>
<?php endif;?>
<?php endforeach; ?>
</li>
<?php $count++; ?>
What you'll probably want to do is starting by iterating through the whole array and grouping each album into a separate array first and making a special array for your album's main photo.
Then you can easily iterate through the arrays to display each album and the code becomes much more maintainable.

Categories