Add Wordpress thumbnails to images attached to pos

2019-09-05 22:18发布

问题:

I am trying to get WordPress to include the thumbnail image within the image tag of any image attached to a post, to make the thumbnail available to the RoyalSlider plugin. (I am hard-coding the plugin into the site - don't want to use the wordpress version for various reasons.)

The end goal is this:

 <img class="rsImg" src="image.jpg" data-rsTmb="small-image.jpg" alt="image description" />

i.e. WordPress displays the images attached to a post, but within each one the relevant thumbnail is added in 'data-rsTmb'.

I would liked to be able to do this via some code inserted into a specific page template, rather than modifying the main loop, as I do not require this behavior for the whole site, just one slider that is created through a custom page template.

I currently have this:

$image = wp_get_attachment_image_src( $attachment_id, 'thumbnail');
/*
$image[0] => url
$image[1] => width
$image[2] => height
*/
echo '<img class="rsImg" src="'. $image[0] .'" data-rsTmb="small-image.jpg" alt="image description" />';

Any help much appreciated!


EDIT: UPDATED TO SHOW CODE I AM USING ON THE SITE BASED ON SUGGESTIONS IN COMMENTS:

<?php

/*
    Template Name: Gallery
*/

?>
<?php query_posts('cat=7&amp;showposts='.get_option('posts_per_page=1')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php 

$image = wp_get_attachment_image_src( $attachment_id, 'full');
$th = wp_get_attachment_image_src( $attachment_id, 'thumbnail');

echo '<img class="rsImg" src="'. $image[0] .'" data-rsTmb="'. $th[0] .'" alt="image description" />';

?>

<p><?php the_content(); ?></p>

<?php endwhile; endif; ?>

<php wp_reset_query(); ?>

EDIT #2: Alternate code from comment in the previous version of this question - this code works, but not for every image (?!):

/*
    Template Name: Gallery
*/

?>

<?php query_posts('cat=7&amp;showposts='.get_option('posts_per_page=1')); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<?php 

$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent='.$post->ID );

foreach ( (array) $images as $imageID => $imagePost ) {
 // Getting the full image size and thumbnail
 $thumbnail = wp_get_attachment_image_src( $imageID, 'thumbnail');
 $full = wp_get_attachment_image_src( $imageID, 'full');

 echo '<img class="rsImg" src="'. $full[0] .'" data-rsTmb="'. $thumbnail[0] .'" alt="'. $imagePost->post_content .'" />';

}
?>

<p><?php the_content(); ?></p>

<?php endwhile; endif; ?>

<php wp_reset_query(); ?>

回答1:

Using your code, try this:

$image = wp_get_attachment_image_src( $attachment_id, 'full');
$th = wp_get_attachment_image_src( $attachment_id, 'thumbnail');

echo '<img class="rsImg" src="'. $image[0] .'" data-rsTmb="'. $th[0] .'" alt="image description" />';

of course you may need some adjustment, specially if you want to use a special size for the big image (instead of full size) but you'll get the idea