0

I have added a image filed in my custom module for taxonomy with name custom_image using add_action( 'taxonomy_name_add_form_fields). And it successfully added and saved with taxonomy.

But how to get it on frontend in my custom template?

EDIT : Used this article https://gist.github.com/mathetos/1eea92f71934442671a7 Here is the code:

<?php

/* Add Image Upload to Series Taxonomy */

// Add Upload fields to "Add New Taxonomy" form
function add_series_image_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="series_image"><?php _e( 'Series Image:', 'journey' ); ?></label>
        <input type="text" name="series_image[image]" id="series_image[image]" class="series-image" value="<?php echo $seriesimage; ?>">
        <input class="upload_image_button button" name="_add_series_image" id="_add_series_image" type="button" value="Select/Upload Image" />
        <script>
            jQuery(document).ready(function() {
                jQuery('#_add_series_image').click(function() {
                    wp.media.editor.send.attachment = function(props, attachment) {
                        jQuery('.series-image').val(attachment.url);
                    }
                    wp.media.editor.open(this);
                    return false;
                });
            });
        </script>
    </div>
<?php
}
add_action( 'weekend-series_add_form_fields', 'add_series_image_field', 10, 2 );

// Add Upload fields to "Edit Taxonomy" form
function journey_series_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( "weekend-series_$t_id" ); ?>

    <tr class="form-field">
    <th scope="row" valign="top"><label for="_series_image"><?php _e( 'Series Image', 'journey' ); ?></label></th>
        <td>
            <?php
                $seriesimage = esc_attr( $term_meta['image'] ) ? esc_attr( $term_meta['image'] ) : ''; 
                ?>
            <input type="text" name="series_image[image]" id="series_image[image]" class="series-image" value="<?php echo $seriesimage; ?>">
            <input class="upload_image_button button" name="_series_image" id="_series_image" type="button" value="Select/Upload Image" />
        </td>
    </tr>
    <tr class="form-field">
    <th scope="row" valign="top"></th>
        <td style="height: 150px;">
            <style>
                div.img-wrap {
                    background: url('http://placehold.it/960x300') no-repeat center; 
                    background-size:contain; 
                    max-width: 450px; 
                    max-height: 150px; 
                    width: 100%; 
                    height: 100%; 
                    overflow:hidden; 
                }
                div.img-wrap img {
                    max-width: 450px;
                }
            </style>
            <div class="img-wrap">
                <img src="<?php echo $seriesimage; ?>" id="series-img">
            </div>
            <script>
            jQuery(document).ready(function() {
                jQuery('#_series_image').click(function() {
                    wp.media.editor.send.attachment = function(props, attachment) {
                        jQuery('#series-img').attr("src",attachment.url)
                        jQuery('.series-image').val(attachment.url)
                    }
                    wp.media.editor.open(this);
                    return false;
                });
            });
            </script>
        </td>
    </tr>
<?php
}
add_action( 'weekend-series_edit_form_fields', 'journey_series_edit_meta_field', 10, 2 );

// Save Taxonomy Image fields callback function.
function save_series_custom_meta( $term_id ) {
    if ( isset( $_POST['series_image'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "weekend-series_$t_id" );
        $cat_keys = array_keys( $_POST['series_image'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['series_image'][$key] ) ) {
                $term_meta[$key] = $_POST['series_image'][$key];
            }
        }
        // Save the option array.
        update_option( "weekend-series_$t_id", $term_meta );
    }
}  
add_action( 'edited_weekend-series', 'save_series_custom_meta', 10, 2 );  
add_action( 'create_weekend-series', 'save_series_custom_meta', 10, 2 );
  • Could you please edit your question to show the code that adds the fields? – Dave Romsey Oct 5 '16 at 7:12
  • Please check my edited question. – Vinaya Maheshwari Oct 5 '16 at 7:19
2

I would suggest to use update_term_meta() and get_term_meta() as they were introduced in WordPress 4.4. This would help to keep your wp_options table smaller.

But either way, you need to know the ID of the term in the "Frontend".

Using Term meta you would need it:

$term_meta = get_term_meta( $term_id, 'series_images', true );

Using your solution you would need it:

$term_meta = get_option( "weekend-series_" . $t_id );

So the question is:

How do I get the ID of a term?

How to get the current term ID on the taxonomies page

A very useful function is get_queried_object(), which returns the queried object. If you are in the taxonomy.php template, or the tag.php or the category.php this would be the current term object:

WP_Term Object
(
    [term_id] => 20
    [name] => Schlagwort
    [slug] => schlagwort
    [term_group] => 0
    [term_taxonomy_id] => 20
    [taxonomy] => post_tag
    [description] => 
    [parent] => 0
    [count] => 0
    [filter] => raw
)

So to get the terms ID in these templates you can do something like:

$current_object = get_queried_object();
$term_id = $current_object->term_id;

How do I get the term IDs of terms attached to a certain post?

If you want to display those images lets say in the single.php template, you would need to get the term IDs attached to the current post. With get_the_terms() you can get exactly those. If you use it inside the loop* you can simply do something like this:

$terms = get_the_terms( get_the_ID(), 'post_tag' );
foreach ( $terms as $term ) {
    $term_id = $term->term_id;
    /* Do something with the $term_id */
}

The first parameter is the current post ID while the second parameter is the slug of the taxonomy (in my example its post_tag for the tags). What you get in return is an array of Term Objects.

How to get the term IDs of all terms of a taxonomy

At last, lets say, you have a taxonomy and you want the term IDs of all the terms attached to this taxonomy get_terms() is your friend.

$terms = get_terms( 'post_tag' );
foreach ( $terms as $term ) {
    $term_id = $term->term_id;
    /* Do something with the $term_id */
}
  • Thanks, get_option( "weekend-series_" . $t_id ); is working for me. I have already term object and term id in my custom template. – Vinaya Maheshwari Oct 5 '16 at 9:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.