将Wordpress缩略图添加到附加到帖子的图像 [英] Add Wordpress thumbnails to images attached to posts

查看:174
本文介绍了将Wordpress缩略图添加到附加到帖子的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让WordPress将缩略图图像包含在附加到帖子的任何图像的图像标记内,以使缩略图可用于RoyalSlider插件。 (我正在将插件硬编码到网站中 - 出于各种原因不想使用wordpress版本。)

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.)

最终目标是:

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

即。 WordPress显示附加到帖子的图像,但在每个帖子中相关的缩略图添加在'data-rsTmb'中。

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.

我目前有这个:

$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" />';

任何帮助非常感谢!

编辑:更新以显示我在网站上使用的代码基于评论中的建议:

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(); ?>






编辑#2:来自评论的替代代码此问题的上一版本 - 此代码有效,但不适用于所有图片(?!):



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(); ?>


推荐答案

使用您的代码,试试这个:

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

这篇关于将Wordpress缩略图添加到附加到帖子的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆