2017-01-05 1 views
1

私はワードプレスで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> 
関連する問題