wordpress acf链接标题不显示

thtygnil  于 2022-12-11  发布在  WordPress
关注(0)|答案(2)|浏览(136)

我有一个链接设置为数组,而不是url。使用以下代码:
<?php $link = get_sub_field('link'); if( $link ): ?> <p><a class="button" href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></p> <?php endif; ?>
链接的页面标题只显示在主页的“注册”框中。在其他页面和其他自定义字段中,链接href会被填充,但标题不会显示。
https://sullivanlehdesigns.com/clients/andonix/testing/卡片上的文本下方应显示一个链接。
转储到内页的结果:
array(3) { ["title"]=> string(0) "" ["url"]=> string(72) "https://sullivanlehdesigns.com/clients/andonix/sign-up-for-a-test-drive/" ["target"]=> string(0) "" }
在主页上:
array(3) { ["title"]=> string(24) "Sign up for a test drive" ["url"]=> string(72) "https://sullivanlehdesigns.com/clients/andonix/sign-up-for-a-test-drive/" ["target"]=> string(0) "" }
模板中的代码:

<?php   // check if the nested repeater field has rows of data
        if( have_rows('card_item') ): 
            // loop through the rows of data ?>
        <section class="cards">
            <?php while ( have_rows('card_item') ) : the_row(); ?>
            <div class="card">
                <?php $image = get_sub_field('image'); 
                echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />'; ?>
                <h3><?php the_sub_field('title'); ?></h3>
                <p><?php the_sub_field('text'); ?></p>

            <?php $link = get_sub_field('link');
                if( $link ): ?>
<p class="button"><a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a></p>
                <?php endif; ?>
            </div>
            <?php endwhile; ?>
        </section>
        <?php endif; ?>
7cwmlq89

7cwmlq891#

修复方法是将后端中链接子字段的“返回值”从“链接URL”更改为“链接数组”
enter image description here

t2a7ltrp

t2a7ltrp2#

如果您已将返回格式设置为“数组”并使用正确的字段名称,则该格式必须有效。可能有以下几个原因:

  • 您尚未填写特定页面上的字段
  • 一个页面上有多个字段具有相同的名称(可以是这样,ACF不会阻止这种情况)
  • 您没有将代码粘贴到正确的位置(例如,post循环之外),并且没有设置当前对象id,则ACF无法正确获取它

有一个免费的插件,称为ACF Views,它允许显示任何ACF字段(包括链接)的简码,而不需要编码。你只需要选择要显示的字段,你会得到一个简码,然后粘贴到任何地方,插件会自动生成字段标记并显示它们。它适用于所有ACF字段类型,并支持所有返回格式,节省了很多时间。
这里有一个他们官方网站的链接-ACF Views: Display ACF fields and Posts。更多的信息沿着他们的YouTube教程和文档的链接可以在那里找到。

相关问题