• Hey,

    I found a way to create custom field for taxonomy terms.
    Now I want to show that field text into a post page that is in that taxonomy. There is any way to do that?
    More on that, I also want to show this taxonomy custom field in page where I see all posts.

    This is my code:

    // Add term page
    function pippin_taxonomy_add_new_meta_field() {
    	// this will add the custom meta field to the add new term page
    	?>
    	<div class="form-field">
    		<label for="term_meta[custom_term_meta]">Imagine</label>
    		<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
    		<p class="description">Introdu linkul imaginii (http://www........../****.jpg)</p>
    	</div>
    <?php
    }
    add_action( 'site_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );
    
    // Edit term page
    function pippin_taxonomy_edit_meta_field($term) {
    
    	// put the term ID into a variable
    	$t_id = $term->term_id;
    
    	// retrieve the existing value(s) for this meta field. This returns an array
    	$term_meta = get_option( "taxonomy_$t_id" ); ?>
    	<tr class="form-field">
    	<th scope="row" valign="top"><label for="term_meta[custom_term_meta]">Link</label></th>
    		<td>
    			<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ); ?>">
    			<p class="description">Link (http://www........../)</p>
    		</td>
    	</tr>
    <?php
    }
    add_action( 'site_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );
    
    // Save extra taxonomy fields callback function.
    function save_taxonomy_custom_meta( $term_id ) {
    	if ( isset( $_POST['term_meta'] ) ) {
    		$t_id = $term_id;
    		$term_meta = get_option( "taxonomy_$t_id" );
    		$cat_keys = array_keys( $_POST['term_meta'] );
    		foreach ( $cat_keys as $key ) {
    			if ( isset ( $_POST['term_meta'][$key] ) ) {
    				$term_meta[$key] = $_POST['term_meta'][$key];
    			}
    		}
    		// Save the option array.
    		update_option( "taxonomy_$t_id", $term_meta );
    	}
    }
    add_action( 'edited_site', 'save_taxonomy_custom_meta', 10, 2 );
    add_action( 'create_site', 'save_taxonomy_custom_meta', 10, 2 );

  • The topic ‘How to list taxonomy custom field on post page?’ is closed to new replies.