Get image inside a post using wp_get_recent_posts - php

I am trying to create a a page in wordpress where I show 5 recent posts.
I was able to successfully do that.
However, I am not able to retrieve images that are present inside that post.
I am using this code to retrieve post url and post image. (Post URL works fine!)
$args = array('numberposts'=>'5');
$recentposts = wp_get_recent_posts($args);
foreach($recentposts as $post){
$v = $post['ID'];
$postlink = get_permalink($v);
$postimg = get_the_post_thumbnail($v);
}

try below code
$posts = get_posts( array( 'posts_per_page' => 5 ) );
foreach ( $posts as $_post ) {
if ( has_post_thumbnail( $_post->ID ) ) {
echo '<a href="' . get_permalink( $_post->ID ) . '" title="' . esc_attr( $_post->post_title ) . '">';
echo get_the_post_thumbnail( $_post->ID, 'thumbnail' );
echo '</a>';
}
}

Related

How to get term/taxonomy image in wordpress?

I want to display all terms with there image of particular taxonomy. i got all term details using get_term() function.
<?php
$terms = get_terms( 'vehicle_type' );
foreach ($terms as $term) :
echo $term->slug;
$colors = apply_filters( 'taxonomy-images-get-terms', '', array(
'taxonomy' => 'vehicle_type',
'term_args' => array(
'slug' => $term->slug,
)
)
);
foreach( (array) $colors as $color) :
echo wp_get_attachment_image( $color->image_id, 'full', array('class' => 'alignnone'));
//echo $term->name;
endforeach;
endforeach;
?>
But it is showing same path for all images.
http://localhost/mototrader/wp-includes/images/media/default.png
How could i get actual path of image associated to that taxonomy.
Thanks in advance.
You can try by this way also
<?php
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";
foreach($catlist as $categories_item)
{
echo '<h1><a href="' . get_category_link( $categories_item->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories_item->name ) . '" ' . '>' . $categories_item->name.'</a> </h1> ';
echo '<div class="categoryoverview clearfix">';
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
foreach( (array) $terms as $term ) {
if($term->term_id == $categories_item->term_id) {
print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'thumbnail' );
echo '</a>';
}
}
echo '<p>'. $categories_item->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
You can Add Advance custom field
<< using that create image field.
<< Assign that image each taxonmoy
<< you can easily get image of corresponding taxonmy image.
Refer this >> https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Trying to call post title, excerpt, and permalink from id for shorcode

I was able to get the title and excerpt to display correctly, but I can't figure out which permalink call to use.
function display_excerpt_shortcode( $atts ) {
extract(shortcode_atts(array(
'excerptid' => ''
), $atts));
if ($excerptid) {
$args=array(
'p' => $excerptid,
'post_type' => 'post',
'post_status' => 'publish'
);
$my_query = new WP_Query($args);
if ($my_query) {
$title = apply_filters( 'the_title', $my_query->posts[0]->post_title );
$excerpt = apply_filters( 'the_excerpt', $my_query->posts[0]->post_excerpt );
$link = apply_filters( 'the_permalink', $my_query->posts[0]->post_permalink );
return '<h3>' . $title . '</h3> <p>' . $excerpt . '</p> <a class="button small primary" href="' . $link . '" title="' . $title . '" >Read More </a>';
}
}
return;
}
add_shortcode('display_excerpt', 'display_excerpt_shortcode');
I've tried all kinds of combinations. the_permalink, get_permalink, post_permalink... I just can't figure out if it's the wrong combination or if I'm just completely off the mark. Thanks in advance.
Have you tried:
$link = get_permalink( $my_query->posts[0]->post_ID )
I think your problem is that the query object has no 'permalink' property.
Following the guide in the codex's class reference page, you'll find the pattern of setting a new post object during each iteration of the loop with $the_query->the_post();:
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//Now reference WP post functions:
the_title();
the_permalink();
}
}
Sorry it's a totally different design, but this has never failed me.

Get image caption in Wordpress

I have this code to retrieve the image url for the images in a gallery and it works ok but i cannot figure out how i could get the caption for each image.
I tried searching all over but i cannot seem to put all the information out there together! Any suggestions on how i could retrieve the captions?
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$src = $image;
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '</a>';
}
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
I hope i explained myself well! Thanks!
This hasn't been tested by try it, I cleaned up some of your code a bit:
1) Combined the first 2 IF statements into 1
2) Used get_post_gallery() (Codex) which returns the src and the image ID. We use the image ID to return the caption, we can also get the description and more if we needed to.
3) Removed the containing Foreach Statement since both my method only returns 1 gallery, not multiple so no need to loop through.
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() || !has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_gallery( $post, false );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through each image in each gallery
$i = 0; // Iterator
foreach( $gallery['src'] as $src ) {
$caption = wp_get_attachment($gallery['id'][$i])['caption'];
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '<div class="caption">'
. $caption
. '</div>'
. '</a>';
$i++; // Incremenet Interator
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
On a sidenote, there is a gallery filter function where you can change how the gallery is displayed post_gallery Filter, here's a question that kind of shows how to edit it. There's also a great WordPress Stack Exchange where that may be helpful in the future!

Grabbing post image - Wordpress PHP

I have a function here which will grab the two most recent post and grab the image posted within those two post. The issue is though, it keeps grabbing the first image of the first post and not also the second:
Code:
function getImageFeatured($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo ''.$image[$num].""; }
$more = 0;
}
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . getImageFeatured('1') .'</a></li> ';
}
Not to sure why It is only sending out the first image of the first post.
Suggestion and thoughts appreciated.
EDIT:
With Revision:
.....above function I posted....
global $post;
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();
This does not work either for me. It seems if I use the setup_postdat... it wont process but if I remove it it goes through but does not do what I ask, but does what I stated the problem to be.
get_the_content is getting the content of the post in the loop. You may run the function on both posts but it still gets the content of the post that's in the loop.
The function you've written needs some serious improvement but for the sake of getting it working in the quickest manner possible replace the existing foreach loop with:
global $post;
foreach( $recent_posts as $post ){
setup_postdata( $post );
echo '<li><a href="' . get_permalink() . '" title="Look '.get_the_title_attribute().'" >' . getImageFeatured('1') .'</a></li> ';
}
wp_reset_postdata();

wordpress get_the_terms not work

this is my query to show my work in the portfolio
<?php
// The Query
$the_query = new WP_Query( array( 'post_type'=> 'portfolio' ) );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$medium = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
$url_thumb = $thumb['0'];
$url_medium = $medium['0'];
$option = '<li>';
$option .= '<a data-value="' . get_the_terms($post->ID, 'portfolio' ) . '" data-largesrc="' . $url_medium .'" data-title="' . get_the_title() .'" data-description="' . get_the_content() .'">';
$option .= '<img src="' . $url_thumb . '" alt="img01" />';
$option .= '</a>';
$option .= '</li>';
echo $option;
}
} else {
}
/* Restore original Post Data */
wp_reset_postdata();
?>
The problem is here, in the data-value I need to extract the category of work
data-value="' . get_the_terms( 'portfolio', $post->ID ) . '"
I think the code that I use is wrong because if I put online I truncates the code and I do not show anything
Your code suggests that portfolio is a custom post type, not a custom taxonomy, but you are passing it as the taxonomy parameter for get_the_terms(). These are not the same things - post types are types of content (e.g. posts, pages) and taxonomies are ways to organize and group things (e.g. tags, categories).
You need to pass the slug of the custom taxonomy as the $taxonomy parameter, not the slug of the custom post type portfolio. I don't know what taxonomy you are querying but it is probably something like portfolio_categories or similar. For example if you were using the default category taxonomy with the post you would want get_the_terms($post->ID, 'category');
You are using wrong syntax, the right syntax is
<?php get_the_terms( $id, $taxonomy ); ?>

Categories