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;?>
Related
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" />';
}
?>
I was hoping someone might be able to help with an issue I'm having...
I'm currently building a Wordpress site and I'm putting together the posts section. Everything seems ok, when using a featured image on a post, it displays nicely in a thumbnail here: http://5.10.105.45/~learningforsusta/index.php/education-for-sustainable-development/ and then nicely inside the single post here: http://5.10.105.45/~learningforsusta/index.php/efsc-post/example-post-1/
However, on a post which doesn't have a featured image specified, it seems to be displaying a default image...
My question is, how can I get rid of the default image, if there is no featured image, I would like it to not display anything...
Here's the code I'm using to pull it all through:
<div class="container">
<div class="row">
<div class="col-md-9">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="page-header">
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);
?>
<p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
<p><em>
By <?php the_author(); ?>
on <?php echo the_time('l, F jS, Y');?>
in <?php the_category( ', ' ); ?>.
<?php comments_number(); ?>
</em></p>
</div>
<?php the_content(); ?>
<hr>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<div class="page-header">
<h1>Oh no!</h1>
</div>
<p>No content is appearing for this page!</p>
<?php endif; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
and here's the code from the functions.php file that relates to the thumbnails...
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size(150, 120, true);
I'm sure I'm missing something simple, but I've tried altering things and i just can't figure it out!
Any help would be greatly appreciated...
Thanks,
Shaun
Make sure that you don't have any function attached to post_thumbnail_html filter, take a look at your functions.php (the best option would be to search in a whole wp-content directory, apply *.php filter to reduce result set)
Make sure you don't have any third-party plugin which can do this behind the scenes, e.g. this one.
You can try by using has_post_thumbnail() function and display with the_post_thumbnail()
<?php if ( has_post_thumbnail()): // Check if thumbnail exists ?>
<p class="featured-image">
<?php the_post_thumbnail('post-thumbnails'); ?>
</p>
<?php endif; ?>
Just check for existence of featured image before outputting it:
<?php
if( has_post_thumbnail() ):
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true );
$thumbnail_meta = get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true);?>
<p class="featured-image"><img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php echo $thumbnail_meta; ?>"></p>
<?php endif;?>
I am working on the home page of this site.
I have successfully placed a featured image thumbnail of the latest blog post in the 3rd column on the right. I am not trying to make the first column featured image thumbnail come from a custom post type I've created called portfolio_item
I've used the following code to place the blog post featured image:
<?php query_posts('showposts=1'); ?>
<?php while (have_posts()): the_post(); ?>
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' ); ?>
<img class="aligncenter circle" alt="" src="<?php echo $image[0]; ?>" />
<?php endif; ?>
<?php endwhile;
wp_reset_query();
?>
What do I need to change to make it retrieve the featured image for the latest post in the custom post type portfolio_item?
query_posts(array ( 'showposts' => 1 , 'post_type' => 'portfolio_item') );
Added this function in any page:
get_template_part('recent', 'posts');
And:
if(have_posts()){
while(have_posts()){ the_post(); ?>
<div class="site-content clearfix"><?php the_content(); ?></div>
<?php }
}
It's working, show 5 blog post on my page. But it's have only title and publish date. I want add thumbnail images on this post.
How can I do it?
You can add something like this to The Loop:
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
Your posts will need to have the Featured Image (formerly called the Post Thumbnail) set.
Edited
More in-depth code:
if( have_posts() ){
while( have_posts() ){
the_post();
if( has_post_thumbnail() ) {
the_post_thumbnail();
}
?>
<div class="site-content clearfix"><?php the_content(); ?></div>
<?php
}
}
Please note, though, that if a post doesn't have a Featured Image set, then the_post_thumbnail() won't get called.
Reference
Post Thumbnails
I use a template that generates thumbnails on the content.php page like so:
<article <?php post_class('single-entry clearfix'); ?>>
<?php
// Test if post has a featured image
if( has_post_thumbnail() ) {
// Get resize and show featured image : refer to functions/img_defaults.php for default values
$wpex_entry_img = aq_resize( wp_get_attachment_url( get_post_thumbnail_id(), 'full' ), wpex_img( 'blog_entry_width' ), wpex_img( 'blog_entry_height' ), wpex_img( 'blog_entry_crop' ) );
?>
<div class="single-entry-thumbnail">
<img src="<?php echo $wpex_entry_img; ?>" alt="<?php echo the_title(); ?>" />
</div><!-- /single-entry-thumbnail -->
<?php } ?>
<div class="entry-text clearfix">
<header>
<h3><?php the_title(); ?></h3>
</header>
</div><!-- /entry-text -->
I am just starting with Wordpress and php and I want to add a parameter to this so only posts with category 'showcase' i.e. will show up. Anyone an idea?
You can use in_category() function to test that post belongs to it.
if ( in_category( 'showcase' ) ) {
...
}
in_category() docs - http://codex.wordpress.org/Function_Reference/in_category