get_template_part function and add thumbnail on Wordpress - php

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

Related

Display images from WordPress post

I have a loop - It is pulling all the posts from category id # 4. I want to upload the image but NOT insert it when i'm writing my post. How can i make it so that i can pull the images uploaded that i uploaded for each post since its in the loop?
<?php query_posts('cat=4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; endif; ?>
I found this link and the code was the answer:
http://www.wpbeginner.com/wp-themes/how-to-get-all-post-attachments-in-wordpress-except-for-featured-image/

Removing feature image from Wordpress post. (Dynamic News lite)

I want my feature image to stop appearing on the post, I simply want to use it as thumbnail on recent post.
before asking here I tried a lots of different solutions.
I look for the responsible code in single.php but it was not there,
I try to hide my feature image with CSS, when I check my site with firebug I notice that my CSS got overwritten by the orginal CSS, which does not happen normally.
After going through my code, I found this in template-tags.php
// Display Custom Header
if ( ! function_exists( 'dynamicnews_display_custom_header' ) ):
function dynamicnews_display_custom_header() {
// Check if page is displayed and featured header image is used
if( is_page() && has_post_thumbnail() ) :
?>
<div id="custom-header" class="featured-image-header">
<?php the_post_thumbnail('custom_header_image'); ?>
</div>
<?php
// Check if there is a custom header image
elseif( get_header_image() ) :
?>
<div id="custom-header">
<img src="<?php echo get_header_image(); ?>" />
</div>
<?php
endif;
}
endif;
// Display Postmeta Data
if ( ! function_exists( 'dynamicnews_display_postmeta' ) ):
function dynamicnews_display_postmeta() {
// Get Theme Options from Database
$theme_options = dynamicnews_theme_options();
// Display Date unless user has deactivated it via settings
if ( isset($theme_options['meta_date']) and $theme_options['meta_date'] == true ) : ?>
<span class="meta-date sep">
<?php printf(__('<time class="entry-date published updated" datetime="%3$s">%4$s</time>', 'dynamicnewslite'),
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() )
);
?>
</span>
<?php endif;
I tried to remove some part of the code here, since I don't know anything about php, I get lots of error, I am hoping may be you guys can help me.
I am using the theme dynamic news lite.
here is the link to my site http://www.coolstuffgift.com/awesome-birthday-gifts-for-awesome-people/
Original code of Single.php
<?php get_header(); ?>
<div id="wrap" class="container clearfix">
<section id="content" class="primary" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post();
get_template_part( 'content', 'single' );
endwhile;
endif; ?>
<?php comments_template(); ?>
</section>
<?php get_sidebar(); ?>
</div>
This is all I got in single.php
I think the developer did some trick to hide the code.
In your content.php OR content-single.php file.
Just comment the_post_thumbnail() this type of function.

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

WordPress category.php not sorting by category when using query_posts

I'm using the SuperSimple Theme for a wordpress blog and am trying to customize the category.php page. I want to have a big image for the latest post in each category on top of a smaller grid of all the older posts.
So far I have it working the way I want, except the top image (div id="post1") is just the most recent post overall instead of the latest post for the category. Here's one of the category pages: http://meanmargie.com/category/hospitality/
And here's the code I have:
<header class="header">
<h1 class="entry-title"><?php single_cat_title(); ?></h1>
<?php if ( '' != category_description() ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . category_description() . '</div>' ); ?>
</header>
<div id="post1">
<?php query_posts('showposts=1'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>
<br>
<div id="post2">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('medium'); } ?>
<div id="post-info"><?php the_title(); ?><br> Read More</div>
<?php endwhile; endif; ?>
</div>
First of all, return the category ID of the category page you are on with get_query_var('cat')
$category = get_query_var('cat');
$cat_id = $category->cat_ID;
Now, return that id back into WP_Query, using 'cat'=> $cat_id,. Please note, showposts is depreciated, use posts_per_page instead
DO NOT USE query_posts. You should never ever use query_posts. It breaks and alters the main query and fails outright in most cases with pagination.
Here's the solution I ended up using:
<div id="post1">
<?php //new query to limit number of posts
$wp_query = new WP_Query();
$wp_query->query($query_string."&posts_per_page=1&paged=".$paged);
?>
<?php WP_Query; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?><?php if ( has_post_thumbnail() ) { the_post_thumbnail('featured'); } ?>
<div class="caption"><?php the_title(); ?></div><div class="excerpt"> Read More</div>
<?php endwhile; endif; wp_reset_query();?>
</div>

How to get Title, Image and excerpt of posts of a specific category Wordpress

I want to get the Post title, image and excerpt of the specific WordPress category. I am using the below code to get the post titles of the specific category.
global $post;
$myposts = get_posts('numberposts=5&category_name=Cooling Towers');
foreach($myposts as $post) :
?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
Any ideas, how do I get post image as well as excerpt for the specific WordPress category?
You can use this
<?php
query_posts( array('posts_per_page'=>5, 'category_name'=>'Cooling Towers') );
while ( have_posts() ) : the_post();
?>
<h2><?php the_title(); ?></h2>
<?php if ( has_post_thumbnail() ): // check for the featured image ?>
<?php the_post_thumbnail(); ?> <!--echo the featured image-->
<?php
endif;
the_excerpt(); // echo the excerpt
endwhile;
wp_reset_query(); // resets main query
?>

Categories