PHP WordPress if else not working unexpected syntax - php

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.

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

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()

Parse error: syntax error, unexpected '{' in if()

I'm getting Parse error: syntax error, unexpected '{'
Here is my code (used in a wordpress comment loop)
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) { ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php } ?>
Try:
<?php if (current_user_can('edit_comment',$comment->comment_ID)) : ?>
<a href="<?php echo get_edit_comment_link(); ?>" title="Edit Comment">
<img src="<?php echo get_template_directory_uri();?>/images/edit-link.png"/>
</a>
<?php endif;?>
I'm a big fan of sticking to one language at a time. Context switches are confusing.
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
printf( "<a href='%s' title='Edit Comment'><img src='%s/images/edit-link.php'/></a>",
get_edit_comment_link(),
get_template_directory_uri()
);
}
?>
Though this is personal preference, and I don't see anything technically wrong with the code in your question.
Try this
<?php
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"get_edit_comment_link()\" title=\"Edit Comment\">
<img src=\"get_template_directory_uri()/images/edit-link.png\"/>
</a>";
} ?>
also why not
<?php $a=get_edit_comment_link(); $b=get_template_directory_uri();
if (current_user_can('edit_comment',$comment->comment_ID)) {
echo "<a href=\"$a\" title=\"Edit Comment\">
<img src=\"$b/images/edit-link.png\"/>
</a>";
} ?>

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.

Categories