WordPress開發中的get_post_custom()函數使用解析,wordpressgetpost_PHP教程

來源:互聯網
上載者:User

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...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.