簡體   English   中英

將自定義分類法元數據字段添加到WordPress

[英]Add custom taxonomy metadata field to WordPress

我正在嘗試在WordPress中為自定義帖子類型添加自定義元數據字段到我的自定義分類術語。 基本上,我想在我的Post類型的類別中添加一個Subtitle字段。

我可以通過掛鈎edit_tag_form_fields`動作將自定義字段添加到我的編輯表單中。

function custom_edit_tag_form_fields( $tag ) {
    $meta_type = 'myCustomPostType_myCustomTaxonomy';
    $object_id = $tag->term_id;
    $meta_key = 'subtitle';
    $single = true;
    $value = get_metadata($meta_type, $object_id, $meta_key, $single);
?>
<tr class="form-field term-description-wrap">
    <th scope="row">
        <label for="subtitle">Subtitle</label>
    </th>
    <td>
        <input name="subtitle" id="subtitle" type="text" value="<?php echo $value; ?>" size="40" />
    </td>
</tr>   
<?php
}

為了保存元數據,我在edited_terms動作中添加了一個鈎子,用於收集和保存輸入的數據。

function custom_edited_terms( $term_id ) {
    $meta_type = 'myCustomPostType_myCustomTaxonomy';
    $object_id = $term_id;  
    $meta_key = 'subtitle';
    $prev_value = null;

    if( isset($_POST[$meta_key]) ) {
        $meta_value = esc_attr( $_POST[$meta_key] );
        update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value);
    }
}

我不確定為什么它沒有存儲到數據庫中。 如果我回顯$value ,我會得到一個空字符串。 我希望它是我在輸入字段中輸入的值。

你讀過有關Wordpress 4.4引入的元數據一詞嗎?

function taxonomy_client_extra_field_url_display( $client ) {

    $client_url = get_term_meta( $client->term_id, 'client-url', true );

    ?>
    <tr class="form-field term-description-wrap">
        <th scope="row">
            <label for="client_url"><?php _e( 'Client URL', 'wp-job-manager' );?></label>
        </th>
        <td>
            <input name="term_meta[client-url]" id="client_url" type="text" value="<?php echo $client_url; ?>" size="40" />
        </td>
    </tr>   
    <?php
}

function taxonomy_client_extra_fields_save( $term_id ) {

    if ( !isset( $_POST['term_meta'] ) ) return;

    foreach ( $_POST['term_meta'] as $slug => $value){

        switch($slug){
            default:
                $value = sanitize_title( $value );
            break;
        }

        update_term_meta( $term_id, $slug, $value );
    }

}

add_action( "job_listing_client_add_form_fields", 'taxonomy_client_extra_field_url_display' );
add_action( "job_listing_client_edit_form_fields", 'taxonomy_client_extra_field_url_display' );

add_action( 'edited_job_listing_client', 'taxonomy_client_extra_fields_save', 10, 2);
add_action( 'created_job_listing_client', 'taxonomy_client_extra_fields_save', 10, 2);

所以我能夠弄清楚我遇到的問題,感謝@WisdmLabs指出我正確的方向。

盡管我的自定義帖子類型具有自定義分類,但它仍然使用原始WordPress表來顯示poststerms 我的問題是我錯誤地為$meta_type使用自定義名稱。 $ meta_type變量與update_metadata使用的表名直接相關。 這就是它沒有保存在數據庫中的原因。 它試圖將其保存到名為'myCustomPostType_myCustomTaxonomy' . 'meta'的表中'myCustomPostType_myCustomTaxonomy' . 'meta' 'myCustomPostType_myCustomTaxonomy' . 'meta'

對於默認分類法,WordPress使用termmeta表,所以我只需要在這兩個函數中更改以下行:

$meta_type = 'myCustomPostType_myCustomTaxonomy';

對此:

global $wpdb;
$meta_type = substr($wpdb->termmeta, strlen($wpdb->prefix), strlen('meta')); // equates to 'term'

$meta_type = 'term';只是硬編碼$meta_type = 'term'; ,我決定采用全局$wpdb數據庫對象中使用的實際wp_prefex_termmeta表,並在WP更改用於分類表的單詞的情況下從那里開始工作以備將來證明。

不幸的是,WordPress中的meta后綴在我正在使用的版本中是硬編碼的,WordPress 4.4。 不過,懷疑很快就會改變。

function custom_edit_tag_form_fields( $tag ) {
    global $post;
    $meta_key = 'subtitle';
    $post_id = $post->ID;
    $value   = get_post_meta( $post_id, $meta_key, true );    
?>
<tr class="form-field term-description-wrap">
    <th scope="row">
        <label for="subtitle">Subtitle</label>
    </th>
    <td>
        <input name="subtitle" id="subtitle" type="text" value="<?php echo $value; ?>" size="40" />
    </td>
</tr>   
<?php
}


function custom_edited_terms( $post_id ) {   
    $meta_key = 'subtitle';
    if( isset($_POST[$meta_key]) ) {
        $meta_value = esc_attr( $_POST[$meta_key] );
        update_post_meta( $post_id, $meta_key, esc_attr( $_POST[meta_key], true ) );
    }
}

暫無
暫無

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

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