首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接到自定义Post类型img src

链接到自定义Post类型img src
EN

Stack Overflow用户
提问于 2015-05-10 14:42:34
回答 1查看 546关注 0票数 1

我在WordPress中设置了一些自定义的post类型,并为图像使用了一个字段。除以下职能外,我还增加了以下内容:

代码语言:javascript
复制
if (function_exists('add_theme_support')) {
    add_theme_support('post-thumbnails');
}
add_image_size( 'homepage-thumb', 190, 190, true);

这给了我预先自定义字段的选项,选择这个作为显示的图像大小,但是我也希望将这个图像链接到完整的大小版本。谁能帮我做这件事,因为我在这里无所适从!

下面是页面的代码:

代码语言:javascript
复制
<?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?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-10 16:18:05

您已经获得了自定义图像大小的URL。使用相同的函数获取完整的图像URL,然后将图像包装在锚标记中。

改变这一点:

代码语言:javascript
复制
<?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]; ?>" />

至:

代码语言:javascript
复制
<?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()之前,请确保已经设置了映像。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30152706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档