如何检测ACF行内容类型


How to detect an ACF row content type

我正试图在wordpress网站上使用fancybox构建一个库。这些图库项目是高级自定义字段(ACFS)中继器。

问题是,客户只希望一些画廊项目是链接,因为有些项目只是带有文本的彩色框,因此不应该是链接,也不应该在fancybox中打开。

正如您在下面的代码中所看到的,我正在调用中继器中的所有行,并将它们放在自己的div中,使用hrefs。

如何检测行是图像还是文本框,并相应地添加href?

<?php
if( have_rows('p3projectsres') ):
    while ( have_rows('p3projectsres') ) : the_row(); ?>
         <div class="s3block">
        <p> <a href="<?php the_sub_field('p3projectreshires'); ?>" rel="lightbox" title="<?php the_sub_field('p3projectresdescription'); ?>">
                <!-- <div class="locationscript"><?php the_sub_field('p3projectreslocation'); ?></div> -->
                <div class="s3blockblurb">
                    <div class="scribe7">   
                        <?php the_sub_field('p3projectresblurb'); ?>
                    </div>
                    <div class="s3blockfaded"><?php the_sub_field('p3projectreslocation'); ?></div>
                </div>
                <img src="<?php the_sub_field('p3projectrespreview'); ?>" /> 
            </a></p>
        </div>
    <?php  endwhile;
else : endif;
?>

请在"我们的工作"下查看此处的问题:www.entirecreave.com/stone

您可以使用get_sub_field()来检索字段的值(而不是用the_sub_field()回显它)。如果未设置该值,它将返回false,因此如果在中继器行上设置了"p3projectreshires"子字段,则它可以在if语句中用于仅输出A打开/关闭标记。您也可以通过选中"p3projectreshires"来有条件地包含预览图像。

<?php
if( have_rows('p3projectsres') ):
    while ( have_rows('p3projectsres') ) : the_row(); ?>
         <div class="s3block"><p> 
         <!-- 
             check to see if there is a value for "p3projectreshires" 
             and if there is open the A tag
         -->
         <?php if ( get_sub_field('p3projectreshires') ) : ?>
             <a href="<?php the_sub_field('p3projectreshires'); ?>" rel="lightbox" title="<?php the_sub_field('p3projectresdescription'); ?>">
         <?php endif; ?>
                <div class="s3blockblurb">
                    <div class="scribe7">   
                        <?php the_sub_field('p3projectresblurb'); ?>
                    </div>
                    <div class="s3blockfaded"><?php the_sub_field('p3projectreslocation'); ?></div>
                </div>
                <!-- Only include the preview image if it is set -->
                <?php if ( get_sub_field('p3projectrespreview') ) : ?>
                     <img src="<?php the_sub_field('p3projectrespreview'); ?>" /> 
                <?php endif; ?>
         <!-- 
             check to see if there is a value for "p3projectreshires" 
             and if there is close the A tag
         -->
         <?php if ( get_sub_field('p3projectreshires') ) : ?>
            </a>
        <?php endif; ?>
        </p></div>
    <?php  endwhile;
else : endif;
?>