似乎无法在主题中提及“大尺寸”、“中型”或“小尺寸”图像。使这个问题更加复杂的是他们的命名约定 100 x ??? 文件名格式,防止硬编码参考。有谁知道任何解决方案?
7610 次
3 回答
4
您要使用的函数是wp_get_attachment_image_src
,但它首先向它传递id
一个有效的附件 id。这是一个如何拉回帖子上的第一个附件的示例,它尝试通过“menu_order”对其进行排序,这是您在“添加媒体”弹出窗口的“图库”选项卡中重新排序项目时设置的顺序:
<?php
function get_attached_images(){
// This function runs in "the_loop", you could run this out of the loop but
// you would need to change this to $post = $valid_post or something other than
// using the global post declaration.
global $post;
$args = array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$attachment = get_posts($args); // Get attachment
if ($attachment) {
$img = wp_get_attachment_image_src($attachment[0]->ID, $size = 'full'); ?>
<img alt="<?php the_title(); ?>" src="<?php echo $img[0] ; ?>" width="<?php echo $img[1] ?>" height="<?php echo $img[2] ?>"/>
<?php }
}
?>
需要注意的重要一点是,您可以在“添加媒体”框中传入"thumbnail"
、和"medium"
,它们对应于相同的尺寸。此外,它返回一个数组:"large"
"full"
[0] => url
[1] => width
[2] => height
编辑:您可以通过在 WordPress 后端的“系统->媒体”下自定义它们来编辑 WordPress 创建的尺寸。
于 2009-12-29T16:06:18.540 回答
2
使用 wp_get_attachment_thumb_url:
<?php
echo wp_get_attachment_thumb_url( $post->ID );
?>
注意:您必须在循环内使用上面的代码段,以便您可以获得 $post 变量。
于 2009-12-29T16:27:51.840 回答
1
好的,供大家以后参考……使用 WordPress 2.9 的新缩略图功能,您可以指定不同的图像大小,如下所示:
<?php the_post_thumbnail('thumbnail'); ?>
<?php the_post_thumbnail('medium'); ?>
等等
叹。这是我想每个搜索和找到此页面的人都会来到的那些“Duh”时刻之一。
Doug Neiner 和 Silent,非常感谢你们提供的想法和答案。我会为你的努力 +1,但事实证明答案比我们想象的要简单。
于 2009-12-29T17:16:50.093 回答