1

このPHP関数を開始しました。この関数は、WordPress のドロップダウン選択メニューに入力することです。

acf/load_field フックは、これを簡単にフックするのに役立ちます。こちらのドキュメントを参照してください。http://www.advancedcustomfields.com/resources/filters/acfload_field/

これは、 post_typeget_postsを照会するために使用する関数です。circuitこのビットは正常に動作します。

下記参照...

function my_circuit_field( $field )
{
    $circuits = get_posts(array(
        "post_type" => "circuit",
        "post_status" => "publish",
        "orderby" => "menu_order",
        "order" => "ASC",
        "posts_per_page"  => -1
    ));
    $field['choices'] = array();
    $field['choices'] = array(
        0 => 'Select a circuit...'
    );
    foreach($circuits as $circuit){
        $field['choices'] = array(
            $circuit->post_title => $circuit->post_title
        );
    }       
    return $field;
}
add_filter('acf/load_field/name=event_calendar_circuit', 'my_circuit_field');



私が抱えている問題は...

$field['choices'] = array(
    0 => 'Select a circuit...'
);

この前に参加していません...

foreach($circuits as $circuit){
    $field['choices'] = array(
         $circuit->post_title => $circuit->post_title
    );
}


ドロップダウンにのみ$circuits foreachが表示されます。ドロップダウン選択メニューの最初のオプションとして「回線を選択」を表示したいと思います。

どこが間違っているのかを理解してくれる人はいますか?

4

2 に答える 2

1

= を使用すると、現在の値が = 記号の後の値に置き換えられます。新しい値を割り当てるたびに、 $field['choices'] の値全体を置き換えています。

あなたはおそらく次のようなことをしたいでしょう

foreach($circuits as $circuit){
    $field['choices'][$circuit->post_title] = $circuit->post_title;
}

ちなみに、$field['choices'] = array();次の行の値を変更するため、この行はコードでは役に立ちません。

于 2013-09-23T13:25:17.913 に答える