doqrjrc95405 2017-03-05 08:32
浏览 82
已采纳

从WordPress选项中获取名称和值

In my functions.php file I've created an options page for WordPress admin which is working fine.

I've got a bunch of fields and I can retrieve the values that are saved and display them on my options page, like this one for example:

$options = get_option( 'flight_settings' );
<input name="passenger_name" value="<?php if($options['passenger_name']) { echo esc_attr_e( $options['passenger_name'] ); } ?>" />

But now I need to take it further and I've hit a road block.

Using this method I can now create additional fields, which can be spawned like...

<input name="passenger_name_1" id="passenger_name_1" value="" />
<input name="passenger_name_2" id="passenger_name_2" value="" />
<input name="passenger_name_3" id="passenger_name_3" value="" />

...and so on, which no limit to how many additional fields you can spawn. This works perfectly fine and when I save the settings the data is being stored in the database (in "flight_settings" option_name with all the correct option values.)

The Actual Problem

The problem I now have is how do I retrieve the stored values without knowing exactly what I'm looking for? With just a single field it's easy because I know the exact name of what I'm looking for (passenger_name for example as mentioned above).

But if 20 fields were spawned and saved, then ideally I would see all 20 fields displayed on my options page.

I'm guessing the answer is something along the lines of a custom loop that looks for all option field names that are similar to passenger_name. But maybe I'm nowhere near on the right track?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dousi1097 2017-03-05 09:56
    关注

    Use an array. If you use the name passenger_name[] for each of your fields, you will get a $_REQUEST['passenger_name'] variable in the backend that is an array. Simply loop through this array.

    This will both simplify what you need to do to spawn an endless amount of such fields, and make it trivial to retrieve their values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部