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

问题是,客户只希望将某些图库项目作为链接,因为有些只是带有文本的彩色框,因此,不应将其作为链接,也不应在幻想框中打开它们。

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

如何检测该行是图像还是文本框,并相应地添加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.entirecreative.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;
?>

关于php - 如何检测ACF行内容类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33429646/

10-11 21:09