Question

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

Was it helpful?

Solution 2

I thanked to @yatendra to help me i got an idea from his answer so its work

here is answer

$queried_object = get_queried_object();
$t_id = $queried_object->term_id;

$term_meta =  get_option( "taxonomy_$t_id" );
echo "<img src=".$term_meta['custom_term_meta']." />";

OTHER TIPS

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 );

I want to customize my category section so have use below code and it is working perfectly without any issue..

Inenter image description here

 function.php





add_action ( 'edit_category_form_fields', function( $tag ){
    $cat_title = get_term_meta( $tag->term_id, '_pagetitle', true ); 

    ?>

    <tr class='form-field'>
        <th scope='row'><label for='cat_page_title'><?php _e('Category Page Title'); ?></label></th>
        <td>
            <input type='text' name='cat_title' id='cat_title' value='<?php echo $cat_title ?>'>
            <p class='description'><?php _e('Title for the Category '); ?></p>
        </td>
    </tr> <?php
});
add_action ( 'edited_category', function() {
    if ( isset( $_POST['cat_title'] ) )
        update_term_meta( $_POST['tag_ID'], '_pagetitle', $_POST['cat_title'] );
});

and call in index.php page for fronted

<?php 
                        $categories = get_categories( array(
                            'orderby' => 'name',
                            'order'   => 'ASC'
                        ) );

                        foreach( $categories as $category ) {
                            if($category->name !="Uncategorized")
                            {

                                $cat_title = get_term_meta( $category->term_id, '_pagetitle', true ); 

                                 echo '
                                 <div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>
                             <div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' .  $category->description . '</a></div>
                             '; 
                            }

                        } 

                        ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top