0
votes

I'm using WordPress with a template that generates a pretty nice thumbnail for each post depending on Ids, and type of post. (ref:https://blinkdemo.wordpress.com/)

Since I've been asked to create a custom page that could show certain post from a category, I decided to create a query for a template page that checks the page slug and then list the posts containing a certain category + a tag ('comparativas').

The problem that I'm facing is that the list of post presented on the page doesn't show up the corresponding thumbnail on each post.

Thumbnails are generated dynamically basically with these lines:

$post_id = $post->ID;
    $thumbnail_id    = get_post_thumbnail_id( $post_id );
    $thumbnail_image = wp_get_attachment_image_src( $thumbnail_id,$thumbnail_size );

The problem is that I couldn't find the way to send to the specific post ids to the function above, since the main $wp_query->posts; retrieves the page id instead of the post requested by the query_posts method.

The loops present the correct posts but when I echo the post->ID it shows the page id.

My query is:

global $wp_query;
 // concatenate the query
 $args = 'cat='.$idCategory.'&tag=comparativas';

 query_posts( $args );
 $posts = $wp_query->posts;
 $current_id = get_the_ID(); //-> this returns the page id

If you could please tell me how to overwrite the global $wp_query; so the template can handle the corresponding ids for the list of post It would be great. Any clue?

Best, Juan

2
Don't use query_posts, use WP_Query().dingo_d
The problem comes within the function that creates the thumbnails. It's a file in a folder called inc/extras.php called link_add_thumbnail_css() that generates each thumbnail for the post checking from where it comes the petition and the id from that post. The home section uses this code and I thought I could reuse that. Unfortunately it retrieves the id from the page but the post ids inside of it. if ( is_page('comparativas') ) { $posts = $wp_query->posts; $count = 0; Thanks in advanceJuan
Still, don't ever use query_posts, just don't...dingo_d
Thanks Dingo_d, I jumped into WP_Query... and Still the function doesn't inject the correct css for the thumbnail. There is a foreach that now I'm feeding with the new query: $the_query; and I'm not sure it's reaching the correct ids: $post_id = $post->ID; $thumbnail_id = get_post_thumbnail_id( $post_id );Juan

2 Answers

0
votes

You could use, setup_postdata($post)

Then get_the_ID() works again :)

0
votes

It doesn't work just because you aren't looping through them. You can do it many ways.

The 2 more common are the following:

  1. overwrite the query with query_posts

$args = array('cat' => $idCategory,'tag' => 'comparativas'); query_posts($args); if(have_posts()){ while(have_posts()){ the_post(); $current_id = get_the_ID(); // this return what you want now the_title(); // this works as expected } } wp_reset_query(); // get previous query back

  1. get an array of WP_Post objects and setup_postdata or simply loop through them

$args = array('cat' => $idCategory,'tag' => 'comparativas');
$posts_i_want = get_posts($args); foreach( $posts_i_want as $post ){ setup_postdata($post); $current_id = get_the_ID(); // this return what you want now the_title(); // this works as expected } wp_reset_postdata(); // get previous postdata back

I personally prefer the first in most cases

Cheers