<?php if ( wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') !='' ) { ?>
<?php echo wp_get_attachment_image( $item['image']['id'], 'home-post-thumbnail' ); } ?>
<?php else {
?> <img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>">
<?php } ?>
I get critical error.phpstorms says that expecting statements.I couldnt find a solution
You cannot "interrupt" the PHP code after the closing { of the if and before the else keyword.
Cleaned up version:
<?php if (wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail') != '') {
echo wp_get_attachment_image($item['image']['id'], 'home-post-thumbnail');
} else {
?><img class="img-responsive img-whp" src="<?php echo plugin_dir_url( __FILE__ ) . '/6-450x450.jpg'; ?>"><?php
} ?>
To avoid such errors, try learning and following a coding standard, e.g. PSR-12.
Since you are working with WordPress, here is WP's coding standard for PHP: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/
Related
How can i show some default code only if BOTH if statements are false ?
I've got this code -
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
i'm not using the else bit at the end at the moment because i only want that to show if both 'if's are false ?
hope that it makes sense
You can use 1 php tag only to make your syntax much more readable and
avoiding
multiple <?php ?> tags.
you can also use echo to print your html markup using php scripting like this.
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
Ok thanks , the 'else' works if neither of the if's are true , but if i have one that is true or both are true , it only displays the first entry but shows it twice ?
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>
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.
I'm trying to create a function that checks for the existence of a post_thumbnail() for a post in Wordpress and sets an element's background image appropriately. Here's what I've tried:
<div class="header-cover-image" style="background-image: url(
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_template_directory_uri() . '/assets/img/article-overlay-1.jpg' }?>
">
</div>
This currently crashes/causes the page to render nothing at all. I'm so confused about when and where to use quotation marks when using PHP within HTML markup!
Any ideas how I can make this work?
Try bloginfo ;
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { echo get_bloginfo('stylesheet_directory') . '/assets/img/article-overlay-1.jpg'; }?>
Cheers!
There was a problem with trying to echo the Wordpress function and append the string simultaneously. Fixed it by doing:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('full');
} else { $uri = get_template_directory_uri(); echo $uri . '/assets/img/article-overlay-1.jpg'; }?>
">
Is there any reason why this is happening due to the following code? All that gets displayed is the variable , i.e the image.
<?php $featured_image = the_post_thumbnail();?>
<?php if (is_page(7) || is_page(12))
echo '<div class="featured_image">' . $featured_image . '</div>'
?>
<?php
$featured_image = the_post_thumbnail();
if (is_page(7) || is_page(12)) {
echo '<div class="featured_image">' . $featured_image . '</div>';
}
?>
The function itself echos the img: http://codex.wordpress.org/Function_Reference/the_post_thumbnail
try:
<?php
if ( has_post_thumbnail() && (is_page(7) || is_page(12))) {
echo '<div class="featured_image">';
the_post_thumbnail();
echo '</div>';
}
?>
What you are seeing is the return value of the echo function.
Here is the code that I am referring to:
<?php if ( is_archive() ) { echo '<img src="'.bloginfo('template_url').'/images/test.png" />'; }?>
This is what the code outputs: http://site.com/wp-content/themes/themename
I'd like it to output the actual image in the code. What part of this did I overlook?
bloginfo() doesn't output the string. It echo's it directly to output stream.
So, the code should be:
<?php if ( is_archive() ) { ?>
<img src="<?php bloginfo('template_url'); ?>/images/test.png" />';
<?php } ?>
Or else, you can use get_bloginfo() :
<?php if ( is_archive() ) { echo '<img src="'.get_bloginfo('template_url').'/images/test.png" />'; }?>
Have you tried this:
$template_url = get_bloginfo('template_url');
<?php if ( is_archive() ) { echo '<img src="'.$template_url.'/images/test.png" />'; } ?>