简体   繁体   English

如何让 WordPress 在不知道名称或长度的情况下提取所有描述元数据字段?

[英]How can I get WordPress to pull all description metadata fields without knowing their names or length?

How can I get WordPress to find and display all of the descriptions in use for a user?如何让 WordPress 找到并显示用户使用的所有描述? Typically, a user on our site will have a description in 2-3 languages.通常,我们网站上的用户会有 2-3 种语言的描述。 The additional description fields in other languages are created by a plugin (and manually filled in by our editors).其他语言的附加描述字段由插件创建(并由我们的编辑手动填写)。 Currently, ANOTHER plugin uses this code to display the main description for a user:目前,另一个插件使用此代码来显示用户的主要描述:

$biography = get_the_author_meta( 'description', $user->user_id )

However, I need it to pull ALL description fields such as:但是,我需要它来提取所有描述字段,例如:

$biography = get_the_author_meta( 'description', $user->user_id ) . $biography = get_the_author_meta( 'description_zh', $user->user_id )

But this is clunky, and I won't know ahead of time how many languages will be added to the site.但这很笨拙,我不会提前知道网站将添加多少种语言。 I thought maybe something like this might work, but no luck:我想也许这样的事情可能会奏效,但没有运气:

$biography = get_the_author_meta( 'description.*', $user->user_id )

I'm new to PHP, so any help much appreciated!我是 PHP 的新手,所以非常感谢任何帮助!

$bio_meta = (array)get_user_meta( $user->user_id );

foreach ( $bio_meta as $key => $value_array ) {
    if ( strpos( $key, 'description' ) !== false ) {
        $description_values[] = $value_array[0];
    }
}
$biography = implode( ' ', $description_values );

Turning the meta results into an array makes life easier, then just check each key that begins with "description."将元结果转换为数组可以让生活更轻松,然后只需检查以“描述”开头的每个键。

If you wanted the output to include the key, you could change如果您希望输出包含密钥,则可以更改

$description_values[] = $value_array[0];

into this:进入这个:

$description_values[] = array( $key => $value_array[0] );

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

相关问题 消费者如何在不知道php Rabbitmq中队列名称的情况下如何同时监听通道中存在的所有队列 - How can consumer listen to all queues present in a channel simultaneously without knowing queue names in php rabbitmq 在 PHP PDO 中,如何在不知道名称的情况下绑定准备好的语句的命名参数? - In PHP PDO, how can I bind named parameters of a prepared statement without knowing their names? 如何在不知道PHP长度的情况下获取字符串的前整数 - How to get the first integers of a string without knowing the length in PHP 如何获取图像的描述元数据? - how to get Description metadata of an image? 如何获得该类中的所有函数名称? (不扩展) - How do I get all the function names in the class? (Without extends) 如何从wordpress的媒体库中获取图像元数据,例如替代文本、标题和描述 - How to get an image metadata such as alt text, caption and description from media library in wordpress 我如何在不知道ID名称的情况下获取php中按钮的ID? - how can i get an ID of a button in php without knowing the ID name upfront 如何在不知道 id 的情况下获取 POST 数据? - How do I get POST data without knowing the id? 如何获取 Symfony 中所有实体的列名? - How can I get the column names of all entities in Symfony? 我如何在Joomla中获得菜单说明 - How can i get menu description in Joomla
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM