How to display the feature image of each post in WordPress? - php

I have used the following code to try and display the featured image for each post, but nothing is showing:
<div class="thumbnail-img">
<?php
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');
if ($lastBlog->has_post_thumbnail()) {
while($lastBlog->has_post_thumbnail()) {
$lastBlog->the_post_thumbnail();
} ?>
<?php get_template_part('content-image', get_the_post_thumbnail());
}
?>
</div>
<br>
<?php
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?>
<?php get_template_part('content-title', get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
</div>
I want the featured image on top of each post title. How do I resolve this?

<?php
$lastBlog = new WP_Query('type=post&posts_per_page=2&offset=1');
if( $lastBlog->have_posts()):
while($lastBlog->have_posts()): $lastBlog->the_post(); ?>
<div class="title"><?php echo get_the_title(); ?></div>
<br />
<div class="thumbnail-img"><?php echo the_post_thumbnail();?></div>
<br />
<?php
endwhile;
endif;
wp_reset_postdata();
?>
try this one should works fine

I cant really be specific about your template structure (template-part content-title??) but using a generic example the following will display the featured image where available;
functions.php
if ( ! function_exists( 'mytheme_setup' ) ) :
function mytheme_setup() {
/*
* Enable support for Post Thumbnails on posts and pages.
*
* See: https://codex.wordpress.org/Function_Reference/add_theme_support#Post_Thumbnails
*/
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 825, 510, true );
}
endif;
add_action( 'after_setup_theme', 'mytheme_setup' );
your content template page (content.php, template-page, etc..)
// WP_Query arguments
$args = array (
'nopaging' => false,
'posts_per_page' => '2',
'offset' => '1',
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article>
<?php if ( has_post_thumbnail() ) : ?>
<div class="post-thumbnail">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="post-title">
<?php echo '<h2>' . get_the_title() . '</h2>'; ?>
</div>
</article>
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
echo "NADA";
}

if you want image url then use this
$thumb_image=wp_get_attachment_url( get_post_thumbnail_id() );
and you want get direct image then here the different different image
the_post_thumbnail( 'thumbnail' ); // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' ); // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' ); // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' ); // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' ); // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' ); // Shop single (600 x 600 hard cropped)

Related

display recent post in sidebar with post thumbnail in wordpress

I am trying to display recent post with thumbnail,title and author name on by website's blog details page's sidebar . title and author is proper but somehow thumbnail image is not showing proper and also when I check in view source, it's not taking thumbnail size 150*150, it's taking image's original size.
check url:- https://stageserver.co/officework/dev/6-lead-generation-techniques-to-immediately-turn-your-business-into-a-sales-magnet-9-3/
This is the code that I tried:-
<!-- Latest News -->
<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div class="sidebar-widget latest-news"><div class="sidebar-title">
<h2>Recent Post</h2>
</div>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="widget-content">
<article class="post">
<div class="post-thumb">
<a href="<?php the_permalink(); ?>" ><img src="<?php
the_post_thumbnail(); ?>" ></img></a>
</div>
<h3><?php the_title(); ?></h3>
<div class="post-info"><?php the_author(); ?></div>
</article>
</div>
<? }
echo '</div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
thanks in advance pls help as soon as possible I am new in php
You can pass the size and other attribute to the the_post_thumbnail( $size, $attr ); function, Default value: 'post-thumbnail'.
Try:
the_post_thumbnail('thumbnail');
the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max)
the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max)
the_post_thumbnail('medium_large'); // Medium Large resolution (default 768px x 0px max)
the_post_thumbnail('large'); // Large resolution (default 1024px x 1024px max)
the_post_thumbnail('full'); // Original image resolution (unmodified)
the_post_thumbnail( array(100,100) ); // Other resolutions
Ok so also the_post_thumbnail() return the image not just the url, so try:
<div class="post-thumb">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('thumbnail'); ?>" >
</a>
</div>

How to use featured image as background image in wordpress

mysite.com
I would like to be able to allow the user of this twentytwelve child theme to be able to upload a featured image to a page (not post) and have that dynamically become the background image (positioned basically the way it is now-see thesite.com)
I think it would need a dynamic url (like wp_get_attachment_thumb_url ), though I'm not using a loop. (was thinking that function has to be used inside a loop.
I am using the code below as a template part. This is what is generating the content on the front page. So basically, I need to get this featured image and use it as a background image for a templage part, and not inside a loop.
<?php $page = get_page_by_title ( 'Welcome' ); ?>
<div id="welcome-intro" class="clear">
<?php if ( get_the_post_thumbnail ( $page->ID ) ) : ?>
<figure class="entry-page-image">
<?php echo get_the_post_thumbnail ( $page->ID, 'full'); ?>
</figure>
<?php endif; ?>
<article id="intro-elements">
<div class="welcome-entry-content">
<?php echo apply_filters( 'the_content', $page->post_content); ?>
</div>
</article>
</div>
/* >>>>>>>>>>>>>>MODIFED<<<<<<<<<<<<<<< */
<?php
// welcome message ?>
<?php $page = get_page_by_title ( 'Welcome' ); ?>
<?php if ( $background = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' ) ) : ?>
<div style="background: url('<?php echo $background[0]; ?>') top right no-repeat; height:400px;" id="welcome-intro" class="clear">
<?php endif; ?>
<article id="intro-elements">
<div class="welcome-entry-content">
<?php echo apply_filters( 'the_content', $page->post_content); ?>
</div>
</article>
</div>`
You can do it in this way by using css
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
<style>
body{
background-image: url('<?php echo $background[0]; ?>');
}
</style>

Display recent posts based on their category in Wordpress

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

Wordpress - post thumbnail in loop

So I'd like to add a thumbnail to my posts but I just can't get it to work.
<?php get_header(); ?>
<div id="main-content">
<?php get_sidebar(); ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('posts_per_page=3&paged=' . $paged);
?>
<?php if (have_posts()) : while ( have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail();?>
<div class="entry">
<?php the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink() ?>">Read More ...</a>
</div>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="postmetadata">
<?php the_tags('Tags: ', ', ', '<br />'); ?>
Posted in <?php the_category(', ') ?> |
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>
</div>
</div>
<?php endwhile; endif; ?>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Posts') ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Posts »') ?></div>
</div>
</div>
<!-- end div main-content -->
<?php get_footer(); ?>
And in my functions.php I've added - add_theme_support('post-thumbnails');
It gives me the option to post the thumbnail when I make a post, but it doesn't show up.
What theme or parent theme are you using? I usually do something like this inside the loop:
<?php
if ( function_exists( 'add_image_size' ) ) {
add_image_size( 'custom-thumb', 180, 115, true ); //add a custom image size
}
echo get_the_post_thumbnail(get_the_ID(), 'custom-thumb', $attr); //echo the thumbnail with the new custom image size
?>
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
Add the above code in loop
Then add the following code to functions.php
add_theme_support( 'post-thumbnails' );
Then at last, if you wish to link your thumbnail to post id, so your posts opens after clicking image, add the following code to functions.php
set_post_thumbnail_size( 50, 50 );
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
set_post_thumbnail_size( height, width); this is used to add height and width, in above example i added 50, 50. Change it with your required value
with the new wordpress versions you can setup thumbnails from settings > media . and give the personnal size to thumbnail Then use this to get the thumbnail with your prefered size
<?php the_post_thumbnail('thumbnail');?>

Sizing images to thumbnail size in WordPress

I'm building my first WordPress Theme and I'm stuck on something.
I have a function in my functions.php called get_first_photo() which grabs the first image uploaded on each post and puts it on the blog archive page. It's working fine, but it loads the full-sized image and resizes it using CSS. I would rather it load the image at it's thumbnail size set in the WP control panel so I don't have the image-size overhead. Any way to accomplish this?
Here's the code from functions.php:
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT guid FROM wp_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And here's the blog template:
<?php
get_header(); ?>
<div id="content" class="blog">
<div id="body">
<h3 class="title" id="blog">The Ned Leary Blog</h3>
<?php if (have_posts()) :
query_posts("category_name=blog");
while (have_posts()) :
the_post();
$first_photo = get_first_photo(get_the_ID());
?>
<div class="snippet">
<?php if (!empty($first_photo)) : ?>
<img src="<?php echo $first_photo; ?>" alt="Thumbnail" />
<?php else : ?>
<img src="<?php bloginfo('url'); ?>/images/blog/gravatarBig.jpg" alt="Ned Leary Gravatar" />
<?php endif; ?>
<div class="details">
<h2><?php the_title(); ?></h2>
<h5><?php the_time('D, M j, Y') ?> by <?php the_author('') ?> | <?php comments_popup_link('0 Comments', '1 Comment', '% Comments'); ?></h5>
<?php the_excerpt(); ?>
<h4>Read more…</h4>
</div>
</div><!--end snippet-->
<?php endwhile; endif;// end of the loop. ?>
</div><!--end body-->
<?php get_sidebar(); ?>
</div><!--end content-->
<?php get_footer(); ?>
All you need is to grab the post ID for that first image you want and then run it through "get_the_post_thumbnail()".
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo );
You can also pull your own custom thumbnail sizes if you like as well. As long as you are using wordpress 2.9+.
Simply add this to functions.php
add_theme_support( 'post-thumbnails' ); //enable thumbs
add_image_size( 'mycustom-preset', width you want, height you want); //custom size
Then run the same function but call your new preset...
$first_photo = post id of the first image;
echo get_the_post_thumbnail( $first_photo, 'mycustom-preset' );
http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
http://codex.wordpress.org/Function_Reference/add_image_size
Hope that helps. It doesn't seem like your having trouble obtaining the post id for the first photo so I didn't really cover how to obtain it.
You can use GD or some other image library to programmatically manipulate an image. However, it would be best to implement an operation to create a thumbnail file when the image is uploaded. You can have it dynamically generate a thumbnail every time the page is loaded, but such an operation can be relatively computationally expensive and put unneeded load on the ssystem.
Thanks to John Ford for pointing me in the right direction, I was able to figure this out.
The query in my function changed slightly, grabbing the post id instead of guid.
function get_first_photo($id) {
global $wpdb;
return $wpdb->get_var("SELECT id FROM aire_posts WHERE post_parent = $id AND post_mime_type = 'image/jpeg' ORDER BY id DESC LIMIT 1");
}
And then I had to add another line to my theme file:
$first_photo = get_first_photo(get_the_ID());
$thumb = wp_get_attachment_image_src($first_photo);
Lastly, I updated my image src:
<img src="<?php echo $thumb[0]; ?>" alt="Thumbnail" />
you could use the get_the_post_thumbnail function or use a php thumbnail generator like timbthumb
<?php $images = get_children( array( 'post_parent' => $page->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
if ( $images ) {
$total_images = count( $images );
$image = array_shift( $images );
$thumbnail = wp_get_attachment_image_src($image->ID, array(225,125) );
$thumbnail = $thumbnail[0]; }
?>
<img class="size-thumbnail alignleft" src="<?php echo $thumbnail; ?>" alt="<?php echo $page->post_title ?>">

Categories