【问题标题】:get first image in wordpress repeater array在 wordpress 中继器数组中获取第一张图片
【发布时间】:2013-03-09 03:24:51
【问题描述】:

我有一个while循环,它获取一个缩略图,然后是一个div中的所有其他图像,用于fancybox。我需要它,以便在单击拇指时,它会在图像的转发器字段中展开第一个图像,而不是初始缩略图。

我尝试将图像取出并回显,但它似乎得到了随机图像。

这是我的代码 firstImage 变量是我用来尝试从转发器字段中获取第一张图像的地方。

这里是网站的链接http://lsmcreative.co.nz/

    <?php query_posts("posts_per_page=-1"); ?>
    <?php if(have_posts()):?>
    <?php $i = 0; ?>
    <?php while(have_posts()) : the_post();?> 

        <?php
        $image1ID = get_field('main');
        $image1 = wp_get_attachment_image_src( $image1ID, 'full-size' );
        $attachment1 = get_post( $image1ID );
        $image1_title = $attachment1->post_title;

        $categories = get_the_category(); 
        $i++;

        $firstImage = get_field('images');
        $firstImageId = $firstImage[0]['image' ]['id' ];
        $imageobject = wp_get_attachment_image_src( $firstImageId, 'full-size'  ); // returns an array
        $image_url = $imageobject[0];           
        ?>                          

            <li class="<?php foreach($categories as $category){ echo $category->category_nicename.' '; } ?>">

                <a class="grouped_elements" rel="group<?php echo $i; ?>" title="<?php echo $image_title; ?>" href="<?php echo $imageobject[0] ?>">
                    <img class="hover" src="<?php bloginfo('template_url') ?>/img/portfolio/hover.jpg" alt="LSM Design">
                    <img src="<?php echo $image1[0] ?>" alt="<?php echo $image1_title; ?>">
                </a>

                <div class="lb-images">

                    <?php if(get_field('images')){ ?>   
                        <?php while(has_sub_field('images')): ?>
                            <?php
                            $imageID = get_sub_field('image');
                            $image = wp_get_attachment_image_src( $imageID, 'gallery-thumb' );
                            $attachment = get_post( $imageID );
                            $image_title = $attachment->post_title;
                        ?>
                        <a class="grouped_elements" rel="group<?php echo $i; ?>"  href="<?php echo $image[0] ?>" title="<?php if(get_sub_field('caption')){the_sub_field('caption');}; ?>"><img src="<?php echo $image[0] ?>" alt="<?php echo $image_title; ?>"></a>
                        <?php endwhile; ?>
                    <?php } ?>  

                </div>

            </li>

    <?php endwhile; ?>
    <?php else: ?>  
    <?php endif; ?>
    <?php wp_reset_query(); ?>  

【问题讨论】:

  • 你能解释一下哪一个是你的转发器字段,以及你正在使用的图片,url,对象或图片ID
  • 我的转发器字段称为图像,我正在尝试获取子字段图像。但是我正在尝试获取该字段中第一张图像的 src 并将其用于 a 标签的 src 在 lb 图像中的 if 和 while 循环之前
  • 它对我来说工作正常,我从变量 $image_url 获取第一个图像 url。而 echo $image_url 你得到了什么价值?
  • 由于某种原因我得到一个空值,如果您检查图像周围的 href,您现在可以在网站上看到它

标签: php wordpress loops get


【解决方案1】:

好的,试试这个可能会起作用,因为你的第一篇文章给你链接可能是由于 get_posts 修改你的代码

$firstImage = get_field('images');

作为

$imid=get_the_ID();
$firstImage = get_field('images',$imid);

希望这会成功.. :)

【讨论】:

  • 我想我看到了你在那里尝试做的事情,但它没有用:(
  • 你能打印_r($firstImage);并将其返回到您未获取图像的部分
  • 数组 ([0] => 数组 ([image] => 80))
猜你喜欢
  • 2011-11-08
  • 2011-12-28
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-05
  • 1970-01-01
  • 2019-12-29
相关资源
最近更新 更多