Ignore thumbnail space if there is no thumbnail - php

So I have the below code that loops through my posts and displays posts in a list format. Here is the code that I'm using:
<?php while (have_posts()) : the_post(); ?>
<div class="one-sixth first"><?php the_post_thumbnail(); ?></div>
<div class="five-sixths"><?php the_title(); ?></div>
<?php endwhile; ?>
Here is the image with the thumbnail:
Here is when no post thumbnail is available, it just leaves an empty space:

If check thumbnail if have displayed otherwise display placeholder image. Hope this help you.
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' )
. '/images/placeholder.jpg" />';
}
?>

Related

hide post thumbnail if there isn't a featured image

i use this code to set the image in related post.
I would, if there isn't a featured image in post, to hidden the thumbnail.
my code is :
<div class="item">
<figure class="entry-image">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'rectangle-size-small' );
} elseif( first_post_image() ) { // Set the first image from the editor
echo '<img src="' . first_post_image() . '" class="wp-post-image" />';
} ?>
</a>
</figure>
<header class="entry-header">
<h4>
<?php the_title(); ?>
</h4>
</header>
</div>
How i replace in line the_post_thumbnail( 'rectangle-size-small' );
to hidden the thumbnail ?
thanks
The has_post_thumbnail() function checks if there is a featured image or not, therefore if there is no featured image provided, it will not be displayed.

How to display the image thumbnail outside of the_content() function in Wordpress

I am making a products page and I want to let the user to upload the image and show it according to the design (the thumbnail is placed at top, then the title and then the description) how can I place it outside the_content(); function
Here's my code:
<section class="products">
<?php query_posts( 'posts_per_page=8' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="products__item">
<div class="products__item--thumbnail">
</div>
<h2 class="products__item--title"><?php the_title(); ?></h2>
<p class="products__item--info"><?php the_content(); ?></p>
<h3 class="products__item--price">60.00</h3>
<span class="fa fa-search products__item-cart"></span>
test
</article>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</section>
Regards!
if you are using featured image and need to display it, place the code bellow where you need the image ( this should be within the loop )
<?php the_post_thumbnail(); ?>
or if you need to get the featured image's link, and display it as a img tag, you can use this code to get the src link of featured image
<?php $image = get_the_post_thumbnail();
Hope this answer helped you any sort

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" />'
; } ?>

Post images printing on default blog page

When I add a image with either feature image or advanced costume fields on each blog post but the images also appear on my default blog page (index.php).
So my blog has 4 posts. My default blog page is showing the 4 blog posts it also displays the image set to each blog post.
Here's my in the header that displays the image:
if (have_posts()) : while (have_posts()) : the_post();
$attachment_id = get_field('banner_image');
if( get_field('banner_image') ):
$image = wp_get_attachment_image_src( $attachment_id, 'banner' );
?><img src="<?php echo $image[0]; ?>" alt="" width ="<?php echo $image[1]?>" height ="<?php echo $image[2]?>" /><?php
endif;
endwhile;
endif;?>
And then the code printing all my blog posts in index.php:
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="news-artical">
<?php the_title('<h1>', '</h1>');
the_excerpt();
//var_dump($post);
?> <hr>
</div> <?php
endwhile;
Would really appreciate some help.
All right. The code in your header loops through all displayed posts on each page. If you want to display the image in header only on single post page, you have to wrap it into condition if ( is_single() ) : or if ( is_singular() ) : if you want to display header image also on single page.
if ( is_single() && have_posts()) : while (have_posts()) : the_post();
$attachment_id = get_field('banner_image');
if( get_field('banner_image') ):
$image = wp_get_attachment_image_src( $attachment_id, 'banner' );
?><img src="<?php echo $image[0]; ?>" alt="" width ="<?php echo $image[1]?>" height ="<?php echo $image[2]?>" /><?php
endif;
endwhile;
endif;?>

How can I check for a thumbnail in WordPress?

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

Categories