Displaying posts thumbnails in Wordpress show only first thumbnail - php

I want to display on the sidebar the latest posts title and thumbnail.
So far I'm getting the posts title and only one thumbnail duplicated.
You can see the result here.(only the first/oldest post image displaying)
Here is my code:
$rps = wp_get_recent_posts($params);
foreach($rps as $rp) :
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachment = current(get_posts( $args ));
?>
<?php echo $rp['post_title'];?><?php echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );?>
<?php endforeach; ?>
Thanks for any tips/assistance given.

Replace 'post_parent' => $post->ID with 'post_parent' => $rp['ID'] . That's it.
What you are doing is, you are passing current post's ID in $args for all posts.

run 2 queries
one outputs the first post. The second outputs everything else excluding the first
<?php $args = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID);
$first = new WP_Query( $args );
while ( $first->have_posts() ) : $first->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $first->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>
<?php $args2 = array( 'post_type' => 'attachment', 'posts_per_page' => 1, 'post_parent' => $post->ID, 'offset' => 1);
$rest = new WP_Query( $args2 );
while ( $rest->have_posts() ) : $rest->the_post(); ?>
<?php the_title();?><?php echo wp_get_attachment_image( $rest->ID, 'thumbnail' );?>
<?php endwhile; wp_reset_postdata(); ?>

Related

wp_reset_query not resetting

I am trying to query several different types of post on the same page. When I try to query the second time (Essays), nothing shows up, meaning my if ($arr_posts->have_posts()) is evaluating as false. The first query and the third and fourth query are working fine. And the second query was working until I added this Interview query before it. And even when I commented it out, it still stopped showing up. What am I missing? And mwp_interview is a custom post type.
<!--Latest Interview-->
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY INTERVIEW POST
</div>
<?php
endif; ?>
<!--Latest Essay-->
<?php
wp_reset_query();
wp_reset_postdata();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Essays in Discipleship',
'posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY ESSAY POST
</div>
<?php
endif; ?>
<!--Latest Special Series-->
<?php
$ss_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Special Post',
'posts_per_page' => 1,
);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );
if ( $ss_arr_posts->have_posts() ) :
$ss_arr_posts->the_post();
?>
<div>
DISPLAY SPECIAL SERIES POST
</div>
<?php
endif;
?>
<!--Latest Podcast-->
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Podcast',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY PODCAST
</div>
<?php
endif;
?>
As per the Wordpress documentation WP_Query Category Parameters.
category_name (string) – use category slug.
The argument's name category_name is actually misleading, category_name is referring to the category SLUG, NOT the actual category NAME.
<?php
$args = [
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo "<hr>";
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'essays-in-discipleship', // ... must be set set to "Essays In Discipleship" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'special-post', // ... must be set to "Special Post" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>

Get first image of custom post type wordpress

I have this code
function display_categoria($args) {
$query = new WP_Query(array(
'post_type' => 'job_listing',
'post_status' => 'publish',
'posts_per_page' => 5
));
while ($query->have_posts()) {
echo $query->the_post();
$id=get_the_id();
echo $query1=get_permalink();
}
wp_reset_query();
}
add_shortcode( 'este', 'display_categoria' );
in theory i can solve it placing in the loop
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
but many entries not have a thumbnail (featured images), Can understand?
This should retrieve the url of the first image for each post. Insert it after the "$id=get_the_id();" line
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'post_status' => null,
'post_parent' => $id
);
$images = get_posts( $args );
if ( $images ) {
$first_image_id = $images[0];
//do something with the image
wp_reset_postdata();
}

How do I display only the first and second most recent post separately without using a loop in PHP?

I need to display separately the first and second most recent posts from a category without using a loop. Below is what I've tried; the item1 div should display the most recent and the item2 div should display the second most recent.
<div class = 'item item1'>
<?php get_posts('numberposts=1&offset=1&category='); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;?>
</div>
<div class = 'item item2'>
<?php get_posts('numberposts=2&offset=1&category='); ?>
<?php while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile;?>
</div>
There is a built-in function for this called: wp_get_recent_posts()
With the following params:
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
Example:
<?php
$args = array( 'numberposts' => '2' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" ><h3>' . $recent["post_title"].'</h3></a></div>';
}
?>
Source: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
You can use get_posts to retrieve two posts and put them in an array to use wherever you like.

Limit posts with attachment inside Wordpress custom post type query

I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called parceiros-e-links).
What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (the_post_thumbnail and the wp_get_attachment_image)
Here is my current code:
<?php
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}
?>
I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first posts_per_page to 1, if the most recent post doesn't has an attachment, no images will be printed!
Any clues about how to limit the number of posts with attachments inside a custom post type?
Thanks!
Your mistakes was at the_post_thumbnail('home-parceiros'); and echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' ); Read about the correct attributes given for this two functions here:
http://codex.wordpress.org/Function_Reference/the_post_thumbnail
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
Here is my suggestion:
$query = new WP_Query(
array(
'post_type' => 'parceiros-e-links',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
)
);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
if(has_post_thumbnail()) : the_post_thumbnail(); endif;
$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 wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile;
endif;
Please let me know :)
Example #2 updated:
$query = new WP_Query( array( 'post_type' => 'parceiros-e-links', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => 2, 'post_parent' => get_the_ID() ) ); //the second loop where I filter the posts with attachments and limit to two
while( $image_query->have_posts() ) { $image_query->the_post();
//code below prints the two attachments: thumbnail and another one.
if(has_post_thumbnail()){
the_post_thumbnail('home-parceiros');
}
echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
}
}
}

Wordpress - Including page title in 'wp get attachment image'

Okay, I've set up a bit of code which searching for all the pages which are a child of the ID 8, then outputs all the attachments (in the gallery) of these pages as unordered list items. You can see the effect so far here http://goo.gl/eq4UF.
The problem I'm having is that I need to include the title of each page before each so you can easily identify which images come below which page. Normally I would just add this in, but the list items al use masonry and are positioned all over the page using some JS so they never appear beside the first image in the list.
I therefore will add the title of the page to every
<li> in the <ul> which will allow the title to run with each image but I don't know how to include this in the wp get attachment image function. Both the_title and wp_title doesn't work inside this loop. apply_filters( 'the_title', $attachment->post_title ); obviously takes the image title, but is there any good to take the page title?
Thanks in advance and hope this made sense,
R
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>
You can try this:
<?php $postslist = get_pages('number=9999&sort_order=DESC&sort_column=post_date&child_of=8');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<ul class="main-projects-list">
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$attachments = get_posts( $args );
if ( $attachments ) {
$post_title = get_the_title($post->ID); // We get the post title
foreach ( $attachments as $attachment ) {
$img_title = apply_filters( 'the_title', $post_title . ' - ' . $attachment->post_title ); // We create the image title with the 2 strings
echo '<li class="each-image">';
echo wp_get_attachment_image( $attachment->ID, 'large' , false, array('title' => $img_title));
echo '<p>';
echo $img_title;
echo '</p></li>';
}
}
?>
</ul>
<?php endforeach; ?>

Categories