簡體   English   中英

如何讓 WordPress 在不知道名稱或長度的情況下提取所有描述元數據字段?

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

如何讓 WordPress 找到並顯示用戶使用的所有描述? 通常,我們網站上的用戶會有 2-3 種語言的描述。 其他語言的附加描述字段由插件創建(並由我們的編輯手動填寫)。 目前,另一個插件使用此代碼來顯示用戶的主要描述:

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

但是,我需要它來提取所有描述字段,例如:

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

但這很笨拙,我不會提前知道網站將添加多少種語言。 我想也許這樣的事情可能會奏效,但沒有運氣:

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

我是 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 );

將元結果轉換為數組可以讓生活更輕松,然后只需檢查以“描述”開頭的每個鍵。

如果您希望輸出包含密鑰,則可以更改

$description_values[] = $value_array[0];

進入這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM