Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress获取自定义字段值_Wordpress - Fatal编程技术网

Wordpress获取自定义字段值

Wordpress获取自定义字段值,wordpress,Wordpress,所以我试图在WP中获得自定义的post值。问题是我可以有多个自定义字段值。所以我不想在代码中硬编码。我想做的是在键值中提供前缀,比如 abc_email abc_website 所以我想用这个函数 get_post_meta 或者其他方法来获取所有以abc开头的键、值对。这样我就可以在后端添加值,而不必更新代码。一种方法是使用上述函数获取所有元数据,然后循环并过滤。但是有没有其他方法可以让我输入我正在寻找的模式 谢谢您应该这样编写代码 $email_meta = get_post_meta(

所以我试图在WP中获得自定义的post值。问题是我可以有多个自定义字段值。所以我不想在代码中硬编码。我想做的是在键值中提供前缀,比如

abc_email
abc_website
所以我想用这个函数

get_post_meta
或者其他方法来获取所有以abc开头的键、值对。这样我就可以在后端添加值,而不必更新代码。一种方法是使用上述函数获取所有元数据,然后循环并过滤。但是有没有其他方法可以让我输入我正在寻找的模式


谢谢

您应该这样编写代码

$email_meta = get_post_meta( 'your_post_id', 'abc_email', true );
$website_meta = get_post_meta( 'your_post_id', 'abc_website', true );

你可以点击这些链接


希望这将对您有所帮助。

您可以直接查询数据库中包含前缀的posts条目(此处的相关表格等)

  • 但这更复杂,仍然涉及到一个循环,并且没有提供任何您可以使用WP和PHP做的事情。如果你想迎合未知,你将不得不循环或互操作索引等
我可能在你的问题中遗漏了什么,但这似乎是直截了当的:

您正试图避免这种情况:

process_field($abc_known1);
process_field($abc_known2);

// continually edit code for new entries e.g.:
process_field($abc_NEW1);
类似这样的东西将处理以后的添加而不进行修改

//  get array of ALL cust fields for post
$all_cust_fields = get_post_meta( get_queried_object_id());
foreach ($all_cust_fields as $key => $value) {
  // some function for determing if $value starts with "abc_"  
  // if so either:
  //     add to an "abc" array for later use/processing
  // or take action in this loop e.g.

   process_field($value);
}

我不确定您谈论的是以“abc_”为前缀的键还是值,但同样的原则也适用。

您看过文档了吗?