dsj8000 2013-04-23 12:42
浏览 58
已采纳

PHP高级自定义字段中继器字段

I have a variable that i want to add doc- to it so it reads out doc-$user_role like this for advanced custom fields in wordpress.

i know how to do this with echo finding it difficult in this format some help please :)

 <?php

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);


        if(get_field('doc-$user_role')): ?>

        <ul>
          <?php while(has_sub_field('doc-$user_role')): ?>

        <li>
         <h3><?php the_sub_field('doc_name'); ?></h3>
           <p><?php the_sub_field('doc_description'); ?></p>
            <a href="<?php the_sub_field('download_doc'); ?>" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/download.png" alt="download_button"></a>

        </li>
    <?php endwhile; ?>
        </ul>

    <?php endif; ?>
  • 写回答

3条回答 默认 最新

  • douxian4323 2013-04-23 13:08
    关注

    There is difference between "" and '' literals. When you put variable in "" it will be parsed by php because it's string. When you put it into '' it's treat as char array and variable will not be parsed that is why you need to use "" literal or sprintf() or you can concate two strings using dot operator.

    so your options are:

      if(get_field("doc-$user_role"))
      if(get_field('doc-'.$user_role))
      if(get_field("doc-".$user_role))
      if(get_field(sprintf("doc-%s", $user_role)))
    

    sprintf is useful when you have long strings and you don't want mess much with code.

    http://php.net/manual/en/function.sprintf.php

    here you have full explanation of how string works in PHP

    http://php.net/manual/en/language.types.string.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调