2017-01-05 30 views
1

我在WordPress上使用ACF。中繼器中的鏈接

我做了一箇中繼器字段。除了鏈接,每個領域都能正常工作。 下面的代碼顯示了URL的名稱,但名稱沒有鏈接!

<?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; ?>"> 

,但我不知道如何解決它。

回答

1

修改標記如下。您試圖訪問尚未聲明的變量,邏輯失序:

<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> 
相關問題