if语句中的Wordpress高级自定义字段

时间:2014-07-27 14:33:16

标签: php wordpress advanced-custom-fields

我试图使用ACF为职位登记板输出不同的工资额。

E.g:

在ACF中选择框选项作为薪资类型("竞争",每天,每年等)。看起来像这样:

enter image description here

然后我有条件字段。所以如果" P.A"选中它会显示P.A字段:

enter image description here

我有一堆这些条件字段用于不同的选择框选项。

我的问题是:

如何将这些添加到if语句中,以便根据提供的内容显示正确的字段?

E.g

if job_salary was selected as "competitive" =
   echo "competitive"
 else if job_salary was selected as "p.a" =
   echo job_salary_singular
 else if job_salary was selected as "p.a range" =
   echo job_range_start " to " job_range_end

希望这是有道理的。这是我目前的代码,它只输出输入的工资类型(例如,竞争性,P.A,每日范围)作为文本。

1 个答案:

答案 0 :(得分:1)

这种方法应该有效。在我的示例中,您选择的主选择器字段,如果它每年支付,竞争,范围等,称为salary_type

然后我得到per_annum。它的出现取决于“每年”和“每年”。从salary_type中选出。最后per_annum_range_lowper_annum_range_high,两者都以“每年范围”为条件。被选中。

然后我们可以根据salary_type选项进行测试,以输出适当的值和HTML。或者,您可以测试这些字段的存在。但我认为这有点清洁,允许你跳过为"竞争"添加额外的字段。

<强> PHP

    <?php

    if (get_field('salary_type')) { //first we check if the salary_type field exists.

        $selection = get_field('salary_type'); //then we store its value as '$selection'

        if ($selection === 'comp') { //we check which selection was made by looking at its label.
            echo '<p>' . 'Competitive' . '</p>';

        } else if ($selection === 'perannum') { //ACF allows you to store a selection as both a value (in this case, 'perannum') and a label ('Per Annum', which is what the user sees.)
            echo '<p>' . get_field('per_annum') . ' per year</p>';

        } else if ($selection === 'perannumrange') {
            echo '<p>From ' . get_field('per_annum_range_low') . ' to ' . get_field('per_annum_range_high') . ' per year</p>'; //and then echo its output and any HTML markup you want.

        } else {
            echo '<p>No Salary Info Given.</p>'; //if they don't make a selection
        }
    }

    ?>