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 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?