Display custom field if it has content or hide if not - php

I'm displaying a custom field like this:
<div class="blog_text">
<img src="<?php the_field('imagen_prensa'); ?>" />
</div>
I would like to make the img appear only if the user has uploaded an image, because right now if there is no image then a broken image symbol appears.
I have tried the following, but it did not work:
<?php if (get_field('imagen_prensa') != ''): ?>
<img src="<?php the_field('imagen_prensa'); ?>" />
<?php endif; ?>
Any can this be achieved?
EDIT:
this is the code now using the answers below but now it gives just a blank page:
<div id="blog_interior">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<div class="article_date"><?php the_time('d/m/Y'); ?></div>
<h2><?php echo get_the_title(); ?></h2>
<div class="blog_text">
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
<div class="video_prensa"><?php the_field('video_prensa'); ?></div>
<?php the_content() ?>
</div>
</article>
<?php endwhile; ?>
<div class="back">
</div>
</div>

empty empty — Determine whether a variable is empty
if (!empty(get_field('imagen_prensa'))) {
// Display image
}

You forgot the echo to actually print the result of the_field('imagen_prensa').
Use this:
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
I've edited your code, to something which seems a bit more logical also added echo's where they were missing. I'm not used to these constructions:
<?php while ( have_posts() ) : the_post(); ?>
So I'm not actually sure if it's working properly or not, but you could check what these variables output by surrounding them with a var_dump like var_dump(have_posts());
Your edited code:
<div id="blog_interior">
<?php /* Start the Loop */ ?>
<?php foreach( $posts as $post ): ?>
<div class="article_date"><?php echo the_time('d/m/Y'); ?></div>
<h2><?php echo get_the_title(); ?></h2>
<div class="blog_text">
<?php if ( !empty(the_field('imagen_prensa')) ): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
<div class="video_prensa"><?php the_field('video_prensa'); ?></div>
<?php echo the_content() ?>
</div>
<?php endforeach; ?>
<div class="back">
</div>
</div>

Try using empty() instead of comparing with an empty string.
<?php if (empty(get_field('imagen_prensa')) === false): ?>
<img src="<?php echo the_field('imagen_prensa'); ?>" />
<?php endif; ?>
empty() also checks for values like null or false which may be returned. You may also trim() the string if the field may contain only whitespaces.

As per your requirement you have set default image as no image in user.
If user have uploaded image then display its own image.
See this link may be help you. PHP: How to display a default Image if the specified one doesn't exists?

Related

PHP Display image issue in WordPress

On this page http://www.cardiffchristmasmarket.com/visit/ i'm trying to get 4 images to display down the side - only 3 images display and when i upload the 4th to the page in WordPress it just removes it instantly. HELP!
PHP of the page below:
<div class="col span_1_of_4">
<div class="page_image">
<?php if( get_field('image_1') ): ?>
<img src="<?php the_field('image_1'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_2') ): ?>
<img src="<?php the_field('image_2'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_3') ): ?>
<img src="<?php the_field('image_3'); ?>" />
<?php endif; ?>
</div>
<div class="page_image">
<?php if( get_field('image_4') ): ?>
<img src="<?php the_field('image_4'); ?>" />
<?php endif; ?>
</div>
</div>
Please check the name "image_4" is match with the id in the custom field
or check the return type of the image in the custom field
Check your wordpress settings, it might be set to display three images only. It may be page settings or widgets, theme settings.
It Seams that you are using ACF (Advanced Custom Fields)
Check this URL for displaying correctly the Images : https://www.advancedcustomfields.com/resources/image/
<?php
$image = get_field('image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
Thanks for your help guys!
The issue lay in the Custom Fields - the field name for image 4 was missing '_'
THANKS!

ACF Repeater in Wordpress not showing images

I'm having trouble getting images shown with a repeater in Wordpress.
For some reason it only shows the alt text instead of the image itself?
example
the upper images are my thumbnail posts and are showing correctly.
Does anyone know whats causing this?
my code
<div class="row">
<div class="col-sm-6 left"><h1>COMMERCIAL</h1><br>
<div class="page-header">
<?php $mymovies = new WP_Query(array(
'post_type' => 'my_movies'
)); ?>
<?php while($mymovies->have_posts()) : $mymovies->the_post(); ?>
<div class="movie-colums">
<div class="thumbtitle group">
<div class="preview">
<?php the_post_thumbnail('thumbnail'); ?>
<h1><?php the_title(); ?><br>
<?php the_field('year'); ?>
<?php
// check if the repeater field has rows of data
if( have_rows('images_rep') ):
// loop through the rows of data
while ( have_rows('images_rep') ) : the_row();
// display a sub field value
the_sub_field('image');
endwhile;
else :
// no rows found
endif;
?>
</h1>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
thanks in advance
Your PHP bit for the repeater stuff should be:
<?php if( have_rows('images_rep') ): ?>
<?php while( have_rows('images_rep') ): the_row();
$image = get_sub_field('image');
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>">
<?php endwhile; ?>
<?php endif; ?>
It looks like your example is outputting the image object so just wrap the image URL in an image tag.
Ref: https://www.advancedcustomfields.com/resources/image/

ACF Wordpress styling custom fields

I have an issue with styling each field of ACF in .php file.
<?php the_field('service_section_title'); ?>
<?php the_field('service_section_description'); ?>
Learn more
<?php if( have_rows('services') ):
while ( have_rows('services') ) : the_row();
$image = get_field('service_icon'); if( !empty($image) ):
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
get_field('service_title');
get_field('service_description');
endwhile; else : endif; ?>
How exactly I should put HTML to wrap my
get_field('service_title');
get_field('service_description');
And Image field;
For example:
<p class="text-faded">?php get_field('service_description');?>
</p>
How to wrap php with html in a correct way (not breaking the php code with php and then interrupting again)
Here is what I suggest you do:
<?php if (have_rows('repeater')): ?>
<?php while (have_rows('repeater')): the_row(); ?>
<?php
$variable = get_sub_field('variable');
?>
<div class="repeater-item">
<div class="variable-item">
<?php echo $variable; ?>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
All you do after this is style everything inside div class="variable-item" and then you can style anything in there.

Conditional PHP if and else statements within existing accordion

I am using Advanced Custom Fields plugin in Wordpress and have managed (with very limited PHP knowledge) to use their repeater fields to create an accordion.
Everything is working really well except the image field (company_logo). So long as the user selects an image for this custom field it displays fine but if they do not select an image I get some strange text instead.
Using my current code I'm trying to add in an 'if' statement so if they do not select an image it displays a default image instead. I've stride lots of variations but cannot get it to work.
Can anyone help/point me in the right direction please? Also, if theres a way I can clean this up as I seem to use a lot of
<div id="accordion">
<?php if( have_rows('exhibitor') ): ?>
<?php while( have_rows('exhibitor') ): the_row(); ?>
<h4 class="accordion-toggle"><?php the_sub_field('exhibitor_type'); ?></h4>
<div class="accordion-content">
<?php while( have_rows('table') ): the_row(); ?>
<div class="exhibitor-single">
<p class="table-field">Table <?php the_sub_field('table_no'); ?></p>
<p><?php the_sub_field('company_name'); ?></p>
<?php $image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<p><?php the_sub_field('company_website'); ?></p>
</div>
<?php endwhile; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
You should be able to just wrap the image 'section' in this:
<?php
if(get_sub_field('company_logo')) :
$image = wp_get_attachment_image_src(get_sub_field('company_logo'), 'logo');
?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_sub_field('company_logo')) ?>" />
<?php
else :
?>
<!-- Your default image here -->
<img src="" alt="" />
<?php
endif; ?>

Adding a fallback image to a wordpress advanced custom fields loop

I'm currently using the advanced custom fields plugin on a wordpress site that I'm developing and it is proving very useful.
I bought the repeater addon which had helped me generate a list of staff members. However I imagine that there won't always be an image available for the staff member so I would like to use a fallback image or use an online placeholder image service like http://placehold.it?
Here is the code:
<?php if(get_field('about_the_practioner')): ?>
<?php while(the_repeater_field('about_the_practioner')): ?>
<article class="colleague_excerpts">
<figure>
<?php if(get_field('image')): ?>
<img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?>
</figure>
<div class="description">
<header>
<h4><?php the_sub_field('header')?><span><?php the_sub_field('title')?></span></h4>
</header>
<p><?php the_sub_field('text') ?></p>
<?php the_sub_field('page_link_text') ?>
</div>
<hr>
</article>
<?php endwhile; ?>
<?php endif; ?>
I've seen conditional statements used to create fallback images if a featured image is not available. I have tried something similar in this case at the point where the image is called, I'm not good with php however and only the fallback image is brought into the webpage even if the image field is populated. Any help would be appreciated.
Just figured it out! The functions that I was passing into the conditional statement were incorrect! I was using the repeater field feature of advanced custom fields and should have been passing in get_sub_field for all the values above. Its the little things that trip you up!
<?php if(get_sub_field('image')): ?>
<img src="<?php the_sub_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?>
Although I don't know what some of these functions return the function get_field('image') may return something no matter if there is an image or not, so instead of
<?php if(get_field('image')): ?>
<img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?>
Maybe try
<?php if(!empty(the_field('image'))): ?>
<img src="<?php the_field('image' ) ?>" alt="<?php the_sub_field('image_alt_text'); ?>">
<?php else: ?>
<img src="http://placehold.it/160x160&text=Awaiting Image">
<?php endif; ?>
Which returns the placeholder if the image is empty.
I might be counting wrong, in fact I was. Ignore this answer. I thought the brackets weren't matching up, and that a problem in the generated HTML was causing a problem. Jonny Beech has worked it out.
Try this
<?php
$image = get_field('banner');
if( !empty($image) ):
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>"/>
<?php else: ?>
<?php
echo "no image";?>
<?php endif; ?>

Categories