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.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?