dsmnedc798226
2014-11-17 18:21 阅读 72
已采纳

WordPress高级自定义字段 - 使用数组中的选择字段和修改数组元素

I'm working on a WordPress website. I am using the ACF Pro plugin. I have a select field and I want to assign a second value to each option. The first value is being used as a heading. The second is a percentage figure (at the moment just a string). This will be used as a label as well as a data-percentage value. I cant get the select values to work with my array. Here is my code.

 <h4>Qualification: <?php echo $array_test; ?></h4>

And then the array;

        <?php
            $myArray = array('$array_tests');
            $myArray[0]= '20%';
            $myArray[1]= '40%';
            $myArray[2]= '60%';
            $myArray[3]= '80%';
            $myArray[4]= '100%';

            echo "<h1> Test:";  
            echo $myArray;
            echo "<h1>";                    

        ?>

And finally using the modified array's output as a data-percentage;

<div class="skillbar clearfix" data-percent="<?php echo $myArray ?>%">
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dtbhp60824 dtbhp60824 2014-11-19 17:35

    So i have assigned this variable to my select custom field.

    $staff_qualification    = get_sub_field('qualification');
    

    I use it for the title.

    <h4>Qualification: <?php echo $staff_qualification; ?></h4>
    

    And I created a switch so that I now have a new value assigned to each option from the dropdown select.

            <?php
    
            switch ($staff_qualification) {
            case "NVQ Level 1":
              $staff_qualification_percentage = 20;
              break;
            case "NVQ Level 2":
              $staff_qualification_percentage = 40;
              break;
            case "NVQ Level 3":
              $staff_qualification_percentage = 60;
              break;
            case "NVQ Level 4":
              $staff_qualification_percentage = 80;
              break;                                
            case "Degree In Childcare":
              $staff_qualification_percentage = 100;
              break;
            }                      
        ?>
    

    And now I echo the variable used in each case as my data-percentage value.

    <div class="skillbar clearfix" data-percent="<?php echo $staff_qualification_percentage; ?>%">
    
    点赞 评论 复制链接分享
  • doudu5029 doudu5029 2014-11-18 14:43

    First of all, you can't echo arrays, but do a print_r($array);

    Point no. 2: You are trying to show $array_test but you are not declaring it beforehand.

    So, to echo the percentage inside the HTML, you should do something like this:

    <?php
                $myArray = array('$array_tests');
                $myArray[0]= '20%';
                $myArray[1]= '40%';
                $myArray[2]= '60%';
                $myArray[3]= '80%';
                $myArray[4]= '100%';
    
                echo "<h1> Test:";  
                print_r ($myArray);
                echo "<h1>";                    
    
            ?>
    
    <div class="skillbar clearfix" data-percent="<?php echo $myArray[1] ?>%">
    

    Here we can echo because you are trying to get one value of the array, and not the whole values.

    This should echo 40% inside the markup "data-percent", however I think this type of CSS bars don't allow the % symbol inside, which means you just need to echo the "40".

    点赞 评论 复制链接分享

相关推荐