get custom field value from taxonomy in wordpress

15,536

Use this

first of all you get category id on taxonomy page.I suppose one category assign to each post.

   $t_id = $term_id;

Then get value using this

   get_option( "taxonomy_".$t_id );
Share:
15,536
Admin
Author by

Admin

Updated on June 05, 2022

Comments

  • Admin
    Admin almost 2 years

    i make a custom field in categories, it save and update data successfully, but i want to show the value of custom field data in archive page, i search a lot about this but in vain Please help me here is my code

    Create custom field:

    add_action ( 'category_add_form_fields', 'extra_field');
    add_action ( 'category_edit_form_fields', 'extra_field');
    function extra_field($term) {    //check for existing featured ID
        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id");
    ?>
        <table width="100%" border="0" cellspacing="3" cellpadding="0" style="margin-bottom:20px;">
            <tr>
                <td><strong>Image</strong></td>
            </tr>
            <tr>
                <td><input type="text" size="40" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>" /></td>
            </tr>
            <tr>
                <td><p>A quick brown fox jumps over the lazy dog.</p></td>
            </tr>
        </table>
    <?php
    }
    

    Save / Update data:

    function save_taxonomy_custom_meta( $term_id ) {
        if ( isset( $_POST['term_meta'] ) ) {
            $t_id = $term_id;
            $term_meta = $_POST['term_meta'];
    
            // Save the option array.
            update_option( "taxonomy_$t_id", $term_meta );
        }
    }
    add_action( 'edited_category', 'save_taxonomy_custom_meta' ); 
    add_action( 'create_category', 'save_taxonomy_custom_meta' );
    

    One more thing, can i make an extra field in db in wp_terms, because it save in wp_options