【问题标题】:Get Image Size Advanced Custom Fields获取图像大小高级自定义字段
【发布时间】:2017-01-06 11:58:41
【问题描述】:

我正在尝试使用高级自定义字段获取带有附件大小的图像 URL。该字段设置为 ID。我已经使用这种方法通过 ID 获取其他图像没有问题。然而,这个并没有拉出渲染的图像。它正在提取 ID 号并将其显示在页面上。我被难住了……

<?php
$the_query = new WP_Query( array(
    'post_type' => 'listicles',
    'tax_query' => array(
        array (
        'taxonomy' => 'visibility',
        'field'    => 'slug',
        'terms' => 'listicles-resortsvisible',
        ),
    ),
) ); 
    $listicleimage = the_field('listicle_featured_image', $post->ID);
    $listicleimgsize = 'listicle-thumb';
    $listicleimg_array = wp_get_attachment_image_src($listicleimage, $listicleimgsize);
    $listicleimg_url = $listicleimg_array[0];
?>

<ul
class="row small-up-1 medium-up-2">

<?php if ( $the_query->have_posts() ) : ?>

<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>


<li class="small-12 medium-6 column">


<img
    src="<?php echo  $listicleimg_url; ?>"
    class="align-center" />

//THE REST OF MY CONTENT//

</li>
<?php endwhile;?>
<?php endif; ?>
</ul>

<?php wp_reset_query(); ?>

【问题讨论】:

    标签: wordpress advanced-custom-fields image-size imageurl


    【解决方案1】:

    你只需要改变

    the_field('listicle_featured_image', $post->ID);
    

    到

    get_field('listicle_featured_image', $post->ID);
    

    【讨论】:

      【解决方案2】:

      您应该使用wp_get_attachment_image 而不是wp_get_attachment_image_src。

      wp_get_attachment_image 接受 ID 作为参数。

      来源:https://developer.wordpress.org/reference/functions/wp_get_attachment_image/

      【讨论】:

        【解决方案3】:

        将此添加到functions.php

        function img_by_id($id, $size, $url) {
                $the_img = wp_get_attachment_image($id, $size);
                if ($url != true) {
                    return $the_img;
                }
                else {
                    $img_src = get_attr($the_img, 'src');
                    return $img_src;
                }
            }
        
        function get_attr($html, $attr) {
            $pc = explode($attr.'="', $html);
            $pc2 = explode('"', $pc[1]);
            return $pc2[0];
        }
        

        然后

        <?php
            $the_query = new WP_Query( array(
                'post_type' => 'listicles',
                'tax_query' => array(
                    array (
                    'taxonomy' => 'visibility',
                    'field'    => 'slug',
                    'terms' => 'listicles-resortsvisible',
                    ),
                ),
            ) ); 
            $listicleimage = the_field('listicle_featured_image', $post->ID);
            $listicleimg_url = img_by_id( $listicleimage, 'listicle-thumb', true);
        
        ?>
        

        如果您需要具有大小的完整图像属性 $img = img_by_id( $listicleimage, 'listicle-thumb', false); 如果您需要 html 标签中的特定属性 $attr = get_attr($html, $attr);

        我的空白 WP 主题模板上有这个。

        【讨论】:

        • 谢谢。它还没有工作。 "src=" 为空白,img 容器中没有图像显示。
        • 也许我们应该在循环中移动$listicleimage = the_field('listicle_featured_image', $post-&gt;ID); $listicleimg_url = img_by_id( $listicleimage, 'listicle-thumb', true);
        • 已移动。出现偏移错误...注意:未定义的偏移量:第 15 行 /Users/robertrhu/Documents/Websites/vail/wp-content/themes/thisisepic/library/image-by-id.php 中的 1
        • 第 15 行有什么?你能粘贴整个代码吗?
        • $pc2 = explode('"', $pc[1]);
        猜你喜欢
        • 2013-10-06
        • 1970-01-01
        • 2015-07-07
        • 1970-01-01
        • 2019-11-07
        • 1970-01-01
        • 1970-01-01
        • 2017-01-14
        • 2018-04-15
        相关资源
        最近更新 更多