How to get thumbnail image in wordpress - php

I have code something like this,
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3>' . get_the_title(). '</h3>';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';
I need to add image function how I can do that? Can you please suggest me.

get_the_post_thumbnail ( int $post_id = null, string|array $size = 'post-thumbnail', string|array $attr = '' )
More info here: https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

Try This
$output .= '<div class="feature-course" '.$style.'>';
$output .= '<h3>' . get_the_title(). '</h3>';
//Display Full image thubnail with link
$output .= ''.get_the_post_thumbnail(get_the_ID(),'full').'';
// Retrieve Thumbnail URL and use it on src attribute
$output .= '<img src="'.wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()) ).'" alt="Yow" title="Yow" />';
$output .= '<p>' . the_excerpt_max_charlength(70). '</p>';
$output .= '<a class="btn-featue" href="' . get_permalink(). '">' . __('View Course', 'themeum-lms'). ' <i class="fa fa-long-arrow-right"></i></a>';

<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>
<?php echo wp_get_attachment_image( 1 ); ?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
echo wp_get_attachment_image( $attachment->ID, 'full' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
https://codex.wordpress.org/Function_Reference/wp_get_attachment_image

You can use the below code for getting thumbnail image.
the_post_thumbnail(); // without parameter -> 'post-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( 'large' ); // Large resolution (default 640px x 640px max)
the_post_thumbnail( 'full' ); // Full resolution (original size uploaded)
the_post_thumbnail( array(100, 100) ); // Other resolutions
For more please referhttps://codex.wordpress.org/Function_Reference/the_post_thumbnail
==========================
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
echo get_the_post_thumbnail($post->ID, 'thumbnail');
echo '</a>';
}

Related

Getting Featured Image as a Background Image

I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
if (is_page('eventsphotography')) {
$query = new WP_query('pagename=eventsphotography');
$eventsphotography_id = $query->queried_object->ID;
//The loop
if($query->have_posts() ) {
while($query->have_posts() ) {
$query->the_post();
the_content();
}
}
/* Get the children of the eventsphotography page */
$args = array (
'post_parent' => $thePostID,
'post_parent' => $eventsphotography_id,
'order' => 'ASC'
);
$eventsphotography_query = new WP_query($args);
//The Loop
if($eventsphotography_query->have_posts() ) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() ){
$eventsphotography_query->the_post();
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
echo '<div class="events-centered">';
echo '<a href="' . get_permalink() . '">';
echo '<h4>' . get_the_title() . '</h4>';
echo '</a>';
echo '<div class="view-events-details">';
echo '<a href="' . get_permalink() . '">';
echo '<h5>View Images</h5>';
echo '</a>';
echo '</div>';
echo '</div>'; /* end of events-centered */
echo '</li>';
}
echo'</ul>';
}
}
?>
I only need help for these lines:
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
AND
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
Here's the screenshot of the result of my code:
http://oi68.tinypic.com/10xzdhl.jpg
I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">
I have found a solution. First, I created a new WP_Query:
$subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));
Then in my loop I added this lines:
if($eventsphotography_query->have_posts() && $subs->have_posts()) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() && $subs->have_posts()){
$eventsphotography_query->the_post();
$subs->the_post();
echo '<li>';
echo get_the_post_thumbnail($post->ID);
...rest of the code...

Calling array in shortcode issues

I am condensing some area of duplicate code over multiple template into one shortcode that I can reuse easily.
I am calling the featured image url as an inline background image style at it works great! Here's the code.
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="single-casestudy" style="background-image: url('<?php echo $image[0]; ?>'); background-size: 100%;">
<div class="case-study-content">
<h4><?php the_title(); ?></h4>
<p><?php echo excerpt(22); ?></p>
</div>
</div>
<?php endwhile ; ?>
This works fine when not in the shortcode, and I can see my featured image as the background on the live site, however when putting it into a shortcode function, it doesn't error, it just doesn't work! Heres the code I am using for the shortcode functionality. It seems like the $image[0] is not pulling through any data into the array.
function otherinfo_function() {
$args = array( 'post_type' => 'casestudy',
'posts_per_page' => -1,
'orderby' => menu_order,
'order' => RAND
);
query_posts($args);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ),'single-post-thumbnail' );
while (have_posts()) : the_post();
echo $image[0];
$output = $output . '<div class="single-casestudy" style="background-image: url(' . $image[0] .'); background-size: 100%;">';
$output = $output . '<div class="case-study-content">';
$output = $output . '<h4>' . get_the_title() . '</h4>';
$output = $output . '<p>' . excerpt(22) . '</p>';
$output = $output . '</div>';
$output = $output . '</div>';
endwhile ;
$output = $output . '</div>';
return $output;
}
add_shortcode('otherinfo', 'otherinfo_function');
If anyone could help that would super great!
Sam

Get Wordpress excerpt in list of posts

I'm building a custom WordPress widget/plugin to return the title and excerpt of a post within a permalink to the article. I am almost there but am struggling with getting the excerpt to show. Here is my current code:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
$excerpt = apply_filters('get_the_excerpt', $recent->post_excerpt);
echo '<a class="m-item diet-and-nutrition" href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" > <div class="pix"></div><div class="eyebrow"> <b>' . $categories[0]->name . '</b> / '. $recent["post_date"].'</div> <figure>' . get_the_post_thumbnail($recent["ID"], 'full') . '<figcaption class="center"> <span> <h4>'. $recent["post_title"].'</h4> </span> <span> <p>'. $excerpt .'</p> </span> </figcaption> </figure> </a>';
}
?>
It's an array so:
$recent->post_excerpt
should be:
$recent['post_excerpt'];

How to show first post content and links for older post only in WordPress

I am trying to create news paper web site and need help to show the content of recent post and title of old posts only... here is my code
<?php
$args = array(
'orderby' => 'id',
'hide_empty' => 1,
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<div class="newsdiv">
<?php
echo '<center><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></center> <br/> ';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {?>
<?php echo"*";the_title(); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>Read more </a></dd>';
echo '</dl>';
?>
</div>
<?php
}
?>
you can try the codex previous post
<?php previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
you can use it in simpler and/or complex way something like this.
<?php previous_post_link(); ?>
for more info you can go through with codex http://codex.wordpress.org/Function_Reference/previous_post_link

Display Author Name (outside of loop)

I have an area of my index.php file that I'm customizing in a theme whereby I would like to display the most recent post and some additional meta. I was able to pull the featured image, post title (and link), & the date; but not the author. I've tried various examples from the WP boards with no luck. The latest attempt below:
<?php
$args = array( 'numberposts' => '1');
$recent_posts = wp_get_recent_posts( $args );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
foreach( $recent_posts as $recent ){
$post_author = get_user_by( 'id', $recent->post_author );
echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';
echo '<div class="row featured-post-meta"><div class="small-8 columns">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2>';
echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
echo '</div></div></div>';
}
?>
Inside your foreach loop:
$post_author = get_user_by( 'id', $recent['post_author'] );
This gets you the user object which looks like what you were trying to do with $curauth.
You could then output $post_author->display_name in place of $curauth.
echo '<p>'. $post_author->display_name .' ...

Categories