How can I check for a thumbnail in WordPress? - php

How can I can I check if a post has a thumbnail and if does do something? If doesn't do something else. This is what I have:
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<?php the_title(); ?>
<?php
}else{
?>
<?php the_post_thumbnail(); ?>
<?php
}
?>
<?php endwhile; ?>
<?php endif; ?>
Any help will be appreciate it.

You already have this, in the line
if ( has_post_thumbnail() )
you are checking if the post has thumbnail, the problems is that you put wrong code in else statement, you have to put something like:
<?php if ( has_post_thumbnail() ) { ?>
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
HAVE THUMBNAIL DO SOMETHING
<?php
}else{
?>
DOESN'T HAVE THUMBNAIL : DO SOMETHING ELSE
<?php
}
?>

Try with these line of codes:
<?php if(has_post_thumbnail())
{
?>
<img src="<?php the_post_thumbnail_url(); ?>" id="contextual" class="contextual" alt="" />
<?php
}
else{
?>
<img src="<?php echo get_template_directory_uri(); ?>/design/images/i-default.jpg" id="contextual" class="contextual" alt="" />
<?php } ?>

To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme’s template files:
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>

firstly CHECK your functions.php file for this
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
if its not in there, copy and paste that into your file..
Secondly Add this to your functions.php
this lets you return the Image src, and not just print the entire img tag
function get_the_post_thumbnail_url( $post_id = NULL ) {
global $id;
$post_id = ( NULL === $post_id ) ? $id : $post_id;
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'full');
$src = $src[0];
return $src;
}
Then on your template page change your code to something like:
this was used as a background image
<?php if ( has_post_thumbnail() ) { ?>
<div id="slider" style="background-image:url(<?php echo get_the_post_thumbnail_url($post->ID, 'large'); ?>); background-position: center center;">
</div>
<?php
}else{
?>
<img src="<?php bloginfo('template_directory');?>/images/blank.jpg" alt="" />
<?php
}
?>
this should produce a div with a background image applied to it,
If you want the Full img tag code to be printed simply use one of the following.
if (has_post_thumbnail()) {
?>
<?php the_post_thumbnail(); // just the image ?>
<?php the_post_thumbnail('thumbnail'); // just the thumbnail ?>
<?php the_post_thumbnail('medium'); // just the Medium Image ?>
<?php the_post_thumbnail('large'); // just the Medium Image ?>
<?php
// adding a 200x200 height and width along with a class to it.
the_post_thumbnail(array( 200,200 ), array( 'class' => 'alignleft' ));
?>
<?php
// Adding a few classes to the medium image
the_post_thumbnail('medium', array('class' => 'alignleft another_class'));
?>
<?php
}
Marty..

Related

trying to insert php into img src attribute to display featured image on custom post archive page in Wordpress

The image is set to return a URL in the Custom Fields Plugin however it is just coming up with img src Unknown, everything else works on the page except this... code is as follows: Thanks in advance for any help!
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'artists',
);
$query = new WP_Query( $args );
?>
<section class="row artists">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-sm-12 col-md-6 col-lg-3">
<a href="<?php if( get_post_field('artist_website') ) { ?>
<?php echo $artist_website; ?>
<?php } else { the_permalink(); } ?>">
<img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
<p><?php echo the_title() ;?></p>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</section>
<?php get_footer(); ?>
The problem is that get_post_field('artist_feature_image') only return the image.
You need to display it with "echo"
<img src="<?php echo get_post_field('artist_feature_image'); ?>" />
And don't do echo the_title() ;, because the_title() is already doing an echo on get_the_title();
function the_title() {
echo get_the_title();
}
So if you want to display it you just have to do: the_title();

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.

Wordpress custom field - css in loop

I try to insert the value of a custom field in a background-image property ( therefore not in img src="..."').
In my category.php; I can display the custom field linked to each post; but when I put the variable in my style ( inline css ), wordpress always displays the same image.
The code :
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<style type="text/css">
.photo_interview {
background-image: url(<?php echo $photo_interview; ?>);
}
</style>
<div class="photo_interview"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
Any idea ? My page here : http://www.overso.me/category/interview/
Your code should be this:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<!-- You don't need style tags if you only want to set the background image -->
<div style="background-image: url(<?php echo $photo_interview; ?>)"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
For what I can see you haven't set the global $post, so get_field do not know which is needed to display. in this case it will be better to use the_ID() to get the current post ID inside the while loop.
Cheers

Check for Wordpress Thumbnail and display dummy if not there

got another question to ask:
I would like to display post thumbnails with the post title underneath. I have managed to work that out through asking on here, however now I would like to add a function that checks for a thumbnail and if none is available displays a dummy image.
Here is my try, which does display the thumbnail (if there) but not the dummy (if no thumbnail is attached):
<div class="gallery_container_wrapper">
<?php query_posts( $args ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="gallery_image_container">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<div class="gallery_image_thumb">
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>
</div>
<div class="gallery_title">
<h2>
<?php the_title(); ?>
</h2>
</div>
</a>
</div>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
What am I doing wrong here?
This code works for me. Yours is virtually identical.
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/images/sunlit_path_banner.jpg" alt="Sunlit Path" />
<?php endif; ?>
Note: I also cut and pasted your code and tested on my WP install, and it worked fine.
Or... try this alternative to the if condition:
<?php if ( get_the_post_thumbnail( get_the_ID() ) ) {
the_post_thumbnail('thumbnail'); }
else {
echo
'<img src="http://www.kunstamkasten.de/wp-content/uploads/2014/08/gallery_dummy.jpg" />'
; } ?>

Wordpress list child page content of current parent

Using the below code I have successfully listed WordPress page titles. the pages have a Featured Image. when I run the below code the image shows up as the Image Unavailable one instead of the actual correct image.
Have I missed something out? The Title displays correctly.
PHP
<?php query_posts(array('showposts' => 30, 'post_parent' => $post->ID, 'post_type' => 'page'));
while (have_posts()) { the_post();
if(has_post_thumbnail()) { ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('medium');?>
</div>
<?php } else { ?>
<div class="entry-thumbnail">
<img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
</div>
<?php } ?>
<?php the_title();
}
wp_reset_query(); // Restore global post data
}?>
has_post_thumbnail() sometimes fails as codex says here http://codex.wordpress.org/Function_Reference/has_post_thumbnail
can you try the following
if ( '' != get_the_post_thumbnail() ) { ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('medium');?>
</div>
<?php } else { ?>
<div class="entry-thumbnail">
<img src="/assets/dummy-image.jpg" alt="Image Unavailable" />
</div>
<?php } ?>
<?php $this_page_id=$wp_query->post->ID; ?>
<?php query_posts(array('showposts' => 20, 'post_parent' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($this_page_id, 'thumbnail') ); ?>
<img src="<?php echo $url ?>" />
<h2><?php the_title(); ?></h2>
<?php } ?>

Categories