Not sure how you do it in your single.php
, but I’m fairly certain not the same way. The main problem is that both the_post_thumbnail()
and get_the_post_thumbnail()
are generating the img
tag output for you – so putting it into the src
attribute of an img
tag like in your code can’t work.
Additionally: The latter does return
, the former does echo
the output. You are using the latter, but don’t echo it so there is actually nothing happening. There are some other differences, e.g. the_post_thumbnail()
can only be used for the current post as it has no $post_id
parameter; get_the_post_thumbnail()
can be used for any post thumbnail, given you input the $post_id
. More on the codex pages.
So the usage in your case would look like this:
// either:
the_post_thumbnail( 'custom-medium' );
// or:
echo get_the_post_thumbnail( get_the_ID(), 'custom-medium' );
Or if you actually want to fill the src
attribute of an img
tag you can do:
<?php
$img_url = wp_get_attachment_image_src(
get_post_thumbnail_id( get_the_ID() ),
'custom-medium'
);
?>
<img src="https://wordpress.stackexchange.com/questions/162764/<?php echo $img_url[0]; ?>">
Making use of wp_get_attachment_image_src()
, get_post_thumbnail_id()
and get_the_ID()
– the latter was already in use in above exemplary code.
Update:
Just to clarify, above mentioned methods do all work – tested and used many times. I assume there must be another problem, but actually can’t tell from the information available.
As proof of concept here a front-page.php
with custom query:
<?php
get_header();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$thumb_args = array(
'posts_per_page' => 1,
'cat' => 87,
'paged' => $paged
);
$thumb_query = new WP_Query( $thumb_args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $thumb_query;
if (
$thumb_query->have_posts()
) {
while (
$thumb_query->have_posts()
) {
$thumb_query->the_post();
the_title();
echo '<br>';
the_post_thumbnail();
echo '<br>';
echo get_the_post_thumbnail();
echo '<br>';
$img_url = wp_get_attachment_image_src(
get_post_thumbnail_id()
);
echo '<img src="'. $img_url[0]. '">';
}
} else {
// no posts
}
echo '<br>';
previous_posts_link( '← ' );
next_posts_link( ' →', $thumb_query->max_num_pages );
wp_reset_postdata();
$wp_query = NULL;
$wp_query = $temp_query;
get_footer();
I additionally had to throw this into the functions.php
to make the pagination work:
add_action(
'pre_get_posts',
'wpse162764_frontpage_custom_query_additional_pagination_fix'
);
function wpse162764_frontpage_custom_query_additional_pagination_fix( $query ) {
if (
is_admin()
) {
return;
}
if (
is_front_page()
) {
$query->set(
'posts_per_page',
1
);
}
}
If this doesn’t work for you, make sure you set the right parameter, than there must be another problem.
Besides, custom queries are barely needed so lets use the main query.
A front-page.php
with main query:
<?php
get_header();
if (
have_posts()
) {
while (
have_posts()
) {
the_post();
the_title();
echo '<br>';
the_post_thumbnail();
echo '<br>';
echo get_the_post_thumbnail();
echo '<br>';
$img_url = wp_get_attachment_image_src(
get_post_thumbnail_id()
);
echo '<img src="'. $img_url[0]. '">';
}
} else {
// no posts
}
echo '<br>';
previous_posts_link( '← ' );
next_posts_link( ' →' );
get_footer();
Additionally you need this in your functions.php
to make it work with the main query:
add_action(
'pre_get_posts',
'wpse162764_pre_get_posts_for_frontpage_main_query'
);
function wpse162764_pre_get_posts_for_frontpage_main_query( $query ) {
if (
is_admin()
|| ! $query->is_main_query()
) {
return;
}
if (
is_front_page()
) {
$query->set(
'posts_per_page',
1
);
$query->set(
'cat',
87
);
}
}
Note:
BTW just to clarify I’ve ran this with multiple queries/loops too, does work and – if done right – can’t be the issue.