WordPress開發中的get_post_custom()函數使用解析,wordpressgetpost
同get_post_meta()一樣,用於返迴文章的自訂欄位值得一個函數,只不過get_post_custom()函數使用起來更簡單,如果在迴圈中使用你甚至不需要設定任何參數。
其實get_post_custom()函數的基礎實現與get_post_meta()大同小異~
get_post_custom()使用
get_post_custom($postid);
只接受一個參數
$postid文章id;
執行個體示範
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom()); endwhile; endif;
輸出的結果如下:(如果如下欄位有設定的話)
array(4) {[“_edit_last”]=>array(1) {[0]=>string(1) “1”}[“_edit_lock”]=>array(1) {[0]=>string(12) “1342451729:1”}[“_thumbnail_id”]=>array(1) {[0]=>string(3) “228”}[“xzmeta”]=>array(2) {[0]=>string(3) “xz1”[1]=>string(3) “xz2”}}
get_post_custom_values和get_post_custom_keys
因為自訂欄位分為,索引值(keys) 和 自訂欄位值(values),有些時候我們需要單獨擷取這兩個值,所以 WordPress 中就派生出了get_post_custom_values和get_post_custom_keys兩個函數,至於意義嘛,我還真沒有發現出有多大意義,除了在大量刪除自訂欄位的時候有一定用除外,我還真沒想出什麼地方能用到,也許在一個浩瀚的 CMS主題中會有著非常劇組輕重的意義。
之前寫到了get_post_custom函數和get_post_meta函數,私下裡想著,反正自訂欄位的相關函數也不多,所以就順手整理了一下,索性把自訂欄位相關的函數都寫一下,當然不包括函數的一些基礎實現代碼。
get_post_custom_values用於擷取當前文章的指定自訂欄位的值,並以數組形式返回。
while (have_posts()) : the_post(); var_dump(get_post_custom_values(‘xzmeta')); endwhile; endif;
大致會返回如下結果
(如果自訂欄位有設定)
array(2) {[0]=>string(3) “xz1”[1]=>string(3) “xz2”}
get_post_custom_keys用於擷取當前文章所有的自訂欄位的索引值。
if (have_posts()) : while (have_posts()) : the_post(); var_dump(get_post_custom_keys()); endwhile; endif;
大致會獲得 以下結果:
(如果自訂欄位有設定)
array(4) {[0]=>string(10) “_edit_last”[1]=>string(10) “_edit_lock”[2]=>string(13) “_thumbnail_id”[3]=>string(6) “xzmeta”}
您可能感興趣的文章:
- WordPress開發中用於擷取近期文章的PHP函數使用解析
- WordPress開發中自訂菜單的相關PHP函數使用簡介
- WordPress中用於擷取搜尋表單的PHP函數使用解析
- 在WordPress中使用wp_count_posts函數來統計文章數量
- 詳解WordPress中調用評論模板和迴圈輸出評論的PHP函數
- 在WordPress中加入Google搜尋功能的簡單步驟講解
- 詳解WordPress開發中的get_post與get_posts函數使用
- 解析WordPress中的post_class與get_post_class函數
- 在WordPress中安裝使用視頻播放器外掛程式Hana Flv Player
- 詳解WordPress中分類函數wp_list_categories的使用
- WordPress開發中短代碼的實現及相關函數提示
http://www.bkjia.com/PHPjc/1088785.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1088785.htmlTechArticleWordPress開發中的get_post_custom()函數使用解析,wordpressgetpost 同get_post_meta()一樣,用於返迴文章的自訂欄位值得一個函數,只不過get_post_cu...