我在WordPress中设置了一些自定义的post类型,并为图像使用了一个字段。除以下职能外,我还增加了以下内容:
if (function_exists('add_theme_support')) {
add_theme_support('post-thumbnails');
}
add_image_size( 'homepage-thumb', 190, 190, true);这给了我预先自定义字段的选项,选择这个作为显示的图像大小,但是我也希望将这个图像链接到完整的大小版本。谁能帮我做这件事,因为我在这里无所适从!
下面是页面的代码:
<?php
$args = array(
'post_type' => 'chairs',
);
$query = new WP_Query( $args );?>
<?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?php
$args = array(
'post_type' => 'chairs',
);
$query = new WP_Query( $args );?>
<?php if( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="item">
<div class="grid-container productmargin-right">
<div class="grid-4 product-image">
<?php $attachment_id = get_field('image');
$size = "homepage-thumb"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
</div>
<div class="grid-8">
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<h3><?php the_field('additional_information'); ?></h3>
</div>
</div>
</div> <!-- end of item -->
<?php endwhile; endif?>发布于 2015-05-10 16:18:05
您已经获得了自定义图像大小的URL。使用相同的函数获取完整的图像URL,然后将图像包装在锚标记中。
改变这一点:
<?php $attachment_id = get_field('image');
$size = "homepage-thumb"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />至:
<?php $attachment_id = get_field('image');
// we don't need a size variable. pass size directly as a parameter.
$thumb_image = wp_get_attachment_image_src( $attachment_id, 'homepage-thumb' );
// get the full size image.
$full_image = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<a href="<?php echo $full_image[0]; ?>">
<img src="<?php echo $thumb_image[0]; ?>" />
</a>添加一些验证也是一个好主意。在尝试运行wp_get_attachment_image_src()之前,请确保已经设置了映像。
https://stackoverflow.com/questions/30152706
复制相似问题