简体   繁体   English

从WordPress选项获取名称和值

[英]Get names and values from WordPress options

In my functions.php file I've created an options page for WordPress admin which is working fine. 在我的functions.php文件中,我为WordPress管理员创建了一个选项页面,该页面工作正常。

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.) 这非常正常,当我保存设置时,数据将存储在数据库中(在“ flight_settings” option_name中具有所有正确的选项值。)

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). 仅需一个字段,这很容易,因为我知道我要查找的确切名称(例如,如上所述的passenger_name)。

But if 20 fields were spawned and saved, then ideally I would see all 20 fields displayed on my options page. 但是,如果生成并保存了20个字段,那么理想情况下,我会在选项页面上看到所有20个字段。

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. 我猜答案是沿着自定义循环的东西,该循环查找与passenger_name相似的所有选项字段名称。 But maybe I'm nowhere near on the right track? 但是,也许我离正确的道路不远了?

Thanks in advance. 提前致谢。

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. 如果对每个字段使用名称passenger_name[] ,则在作为数组的后端中将获得$_REQUEST['passenger_name']变量。 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. 这样既可以简化生成无数此类字段所需的操作,又可以轻松地检索它们的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM