转发器中的链接

Links in repeater

我在 WordPress 上使用 ACF。

我做了一个中继域。除了 links 之外,每个字段都工作正常。 下面的代码显示了 URL 的名称,但是名称中没有 link!

<?php if( have_rows('dl_box') ): ?>

    <ul>

    <?php while( have_rows('dl_box') ): the_row(); 

        // vars
        $content = get_sub_field('dl_link_name');
        $link = get_sub_field('dl_url');

        ?>

        <li>
        <span class="link">
            <?php if( $link ): ?>
                <a href="<?php echo $url; ?>">
            <?php endif; ?>
                        <?php if( $link ): ?>
            </a>

            <?php endif; ?>
   <?php echo $content; ?>

    </span> 
        </li>

    <?php endwhile; ?>

    </ul>

<?php endif; ?>

我觉得是因为这条线

<a href="<?php echo $url; ?>">

但是我不知道怎么解决。

修改标记如下。您正在尝试访问尚未声明的变量,并且逻辑顺序不正确:

<li>
    <span class="link">
        <?php 
        // $link is the URL (from "dl_url")
        // If there is a URL, output an opening <a> tag
        if( $link ) {
            echo '<a href="' . $link . '">';
        }
        // $content is the name (from "dl_link_name")
        // always output the name
        echo $content;
        // If there is a URL, need to output the matching closing <a> tag
        if( $link ) {
            echo '</a>';
        }
    </span> 
</li>

注:
我已经学会了不喜欢那样的标记/逻辑——它没有多大意义。我宁愿做这样的事情——它更简单、更容易阅读、也更紧凑:

<li>
    <span class="link">
         <?php
         // if there is a url, output the ENTIRE link
         if ( $link ) {
             echo '<a href="' . $link . '">' . $content . '</a>';
         // otherwise just output the name
         } else {
             echo $content;
         } ?>
     </span>
 </li>