全代碼實現WordPress分類目錄和標簽添加新的自定義字段


WordPress的分類目錄默認只有名稱、別名、父節點和描述這幾個字段,有時候我們需要給分類目錄拓展一些信息,如想添加一個分類封面圖、給分類指定keywords和description等等,這個時候我們就得給分類目錄添加自定義字段(或者叫自定義欄目)。本文將給你介紹如何給WordPress的分類目錄和標簽添加新的自定義字段。

 

插件實現

本文介紹的重點不是插件,但是如果你不會寫代碼,或者喜歡更方便的插件,推薦下面幾款插件:

  • LSD Custom taxonomy and category meta
  • Custom taxonomy meta
  • Category Meta plugin
  • Category Thumbnails (該插件只能實現添加分類封面的效果)

 

全代碼實現分類加字段

將下面的php代碼復制粘貼到你當前主題的 function.php 中即可。以下代碼只給分類目錄添加自定義字段,如果需要給標簽添加自定義字段,請看文章后面的講解。這部分代碼包括 4 大塊:調用WordPress的action;新建分類頁面添加自定義字段輸入框;編輯分類頁面添加自定義字段輸入框;保存自定義字段的數據。這里只創建一個分類封面的URL輸入框。如果要添加更多的自定義字段,也只需在代碼中幾個 TODO 的位置上追加一些代碼而已,代碼中也給出添加keywords字段的示例。

所有自定義字段保存在WordPress的_options表中,無需建新的表。

<?php

class Ludou_Tax_Image{
 
    function __construct(){
        
        // 新建分類頁面添加自定義字段輸入框
        add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
        // 編輯分類頁面添加自定義字段輸入框
        add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

        // 保存自定義字段數據
        add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
        add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );
 
 
    } // __construct
 
    /**
     * 新建分類頁面添加自定義字段輸入框
     */
    public function add_tax_image_field(){
    ?>
        <div class="form-field">
            <label for="term_meta[tax_image]">分類封面</label>
            <input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="" />
            <p class="description">輸入分類封面圖片URL</p>
        </div><!-- /.form-field -->
        
        <!-- TODO: 在這里追加其他自定義字段表單,如: -->
        
        <!--
        <div class="form-field">
            <label for="term_meta[tax_keywords]">分類關鍵字</label>
            <input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="" />
            <p class="description">輸入分類關鍵字</p>
        </div>
        -->
    <?php
    } // add_tax_image_field
 
    /**
     * 編輯分類頁面添加自定義字段輸入框
     *
     * @uses get_option()       從option表中獲取option數據
     * @uses esc_url()          確保字符串是url
     */
    public function edit_tax_image_field( $term ){
        
        // $term_id 是當前分類的id
        $term_id = $term->term_id;
        
        // 獲取已保存的option
        $term_meta = get_option( "ludou_taxonomy_$term_id" );
        // option是一個二維數組
        $image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
        
        /**
         *   TODO: 在這里追加獲取其他自定義字段值,如:
         *   $keywords = $term_meta['tax_keywords'] ? $term_meta['tax_keywords'] : '';
         */
    ?>
        <tr class="form-field">
            <th scope="row">
                <label for="term_meta[tax_image]">分類封面</label>
                <td>
                    <input type="text" name="term_meta[tax_image]" id="term_meta[tax_image]" value="<?php echo esc_url( $image ); ?>" />
                    <p class="description">輸入分類封面圖片URL</p>
                </td>
            </th>
        </tr><!-- /.form-field -->
        
        <!-- TODO: 在這里追加其他自定義字段表單,如: -->
        
        <!--
        <tr class="form-field">
            <th scope="row">
                <label for="term_meta[tax_keywords]">分類關鍵字</label>
                <td>
                    <input type="text" name="term_meta[tax_keywords]" id="term_meta[tax_keywords]" value="<?php echo $keywords; ?>" />
                    <p class="description">輸入分類關鍵字</p>
                </td>
            </th>
        </tr>
        -->
        
    <?php
    } // edit_tax_image_field
 
    /**
     * 保存自定義字段的數據
     *
     * @uses get_option()      從option表中獲取option數據
     * @uses update_option()   更新option數據,如果沒有就新建option
     */
    public function save_tax_meta( $term_id ){
 
        if ( isset( $_POST['term_meta'] ) ) {
            
            // $term_id 是當前分類的id
            $t_id = $term_id;
            $term_meta = array();
            
            // 獲取表單傳過來的POST數據,POST數組一定要做過濾
            $term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_url( $_POST['term_meta']['tax_image'] ) : '';

            /**
             *   TODO: 在這里追加獲取其他自定義字段表單的值,如:
             *   $term_meta['tax_keywords'] = isset ( $_POST['term_meta']['tax_keywords'] ) ? $_POST['term_meta']['tax_keywords'] : '';
             */

            // 保存option數組
            update_option( "ludou_taxonomy_$t_id", $term_meta );
 
        } // if isset( $_POST['term_meta'] )
    } // save_tax_meta
 
} // Ludou_Tax_Image
 
$wptt_tax_image = new Ludou_Tax_Image();

  

資源網站大全 https://55wd.com 設計導航https://www.wode007.com/favorites/sjdh

如果需要在主題中調用分類自定義字段的值,可以使用以下代碼:

// $term_id 是當前分類的id,自行想辦法獲取
$term_id = $term->term_id;
        
// 獲取已保存的option
$term_meta = get_option( "ludou_taxonomy_$term_id" );

// 取值
$tax_image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';

  

 

全代碼實現標簽加字段

給標簽添加自定義字段的原理是一樣的,只需把上面第一部分代碼中的action修改一下即可,將以上代碼中的:

// 新建分類頁面添加自定義字段輸入框
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
// 編輯分類頁面添加自定義字段輸入框
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

// 保存自定義字段數據
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );

  

 

改成:

// 其實就是把 category 改成 post_tag 即可
add_action( 'post_tag_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'post_tag_edit_form_fields', array( $this, 'edit_tax_image_field' ) );

add_action( 'edited_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );

  

 

另外,也可以同時給分類目錄和標簽添加自定義字段:

// 分類
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );

// 標簽
add_action( 'post_tag_add_form_fields', array( $this, 'add_tax_image_field' ) );
add_action( 'post_tag_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
add_action( 'edited_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_post_tag', array( $this, 'save_tax_meta' ), 10, 2 );

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM