Support

Account

Home Forums Backend Issues (wp-admin) Term field in functions.php

Helping

Term field in functions.php

  • Hi,

    I’m struggling with a problem and hope someone can help me with this.

    I have a field called taxonomy_image which is an Image Field Type which triggers on all Taxonomy Terms. The return value is Image URL.
    I have no problem getting the images in front end, but I would also like the associated image to turn up in admin on edit-tags.php, i.e. as a column in the term list.

    Getting the new column to turn up in the right place is no problem, but I can’t seem to figure out how to retrieve the actual image url. All i get is src(unknown).

    Here is my code at the moment

    if ( ! function_exists( 'catchimage_column' ) ):
    // Prepend the new column to the columns array
    function catchimage_column($cols) {
        $column_id = array( 'catchimage' => __( 'Image', 'catch-ids' ) );
        $cols = array_slice( $cols, 0, 30, true ) + $column_id + array_slice( $cols, 1, NULL, true );
        return $cols;
    }
    endif; // catchimage_column
    
    if ( ! function_exists( 'catchimage_return_value' ) ) :
    function catchimage_return_value($value, $column_name) {
            if ( 'catchimage' == $column_name ) {
    // ------ Here is the part that doesn’t work
                $queried_object = get_queried_object(); 
                $taxonomy = $queried_object->taxonomy;
                $term_id = $queried_object->term_id;
                $thumbnail = get_field('taxonomy_image', $taxonomy . '_' . $term_id);
                    $value .= '<img class="taxonomy-image" src="'. $thumbnail .'" />';
            }
            return $value;
        }
    endif;
    
    if ( ! function_exists( 'catchimage_css' ) ) :
    // Output CSS for width of new column
    function catchimage_css() {
    ?>
    <style type="text/css">
        #catchimage {width: 80px;}
        @media screen and (max-width: 782px) {
            .wp-list-table #catchimage, .wp-list-table #the-list .catchimage {display: none;}
            .wp-list-table #the-list .is-expanded .catchimage {padding-left: 30px;}
        }
    </style>
    <?php
    }
    endif; // catchimage_css
    add_action( 'admin_head', 'catchimage_css');
    
    if ( ! function_exists( 'catchimage_add' ) ) :
    // Actions/Filters for various tables and the css output
    function catchimage_add() {
    
        // For Category, Tags and other custom taxonomies Management
        foreach( get_taxonomies() as $taxonomy ) {
            add_action( "manage_edit-${taxonomy}_columns" ,  'catchimage_column' );
            add_filter( "manage_${taxonomy}_custom_column" , 'catchimage_return_value', 10 , 3 );
            if( version_compare($GLOBALS['wp_version'], '3.0.999', '>') ) {
                add_filter( "manage_edit-${taxonomy}_sortable_columns" , 'catchimage_column' );
            }
        }
    
    }
    endif; // catchimage_add
    add_action( 'admin_init', 'catchimage_add' );

    —-
    I’ve also tried with;

    if ( ! function_exists( 'catchimage_return_value' ) ) :
    function catchimage_return_value($value, $column_name, $term_id) {
            if ( 'catchimage' == $column_name ) {
                global $feature_groups, $taxonomy;
                $term_id = absint( $term_id );
                $feature_group = get_term_meta('taxonomy_image', $taxonomy . '_' . $term_id);
                $value .= '<img class="taxonomy-image" src="'. esc_attr( $feature_groups[ $feature_group ] ) .'" />';
            }
            return $value;
        }
    endif;

    As well as the same code as in front end;

    $terms = get_the_terms( get_the_ID(), 'chef');
    $term = array_pop($terms);
    $custom_field = get_field('taxonomy_image', $term );
        if( !empty($custom_field) ) {
        echo '<img class="taxonomy-image" src="'.$custom_field.'" />';
        }
    else {
        //Do nothing
    }

    But that didn’t do the trick either.
    I read somewhere that this might not be achievable from functions.php so I moved it into a plugin, but that didn’t change anything.
    Sooo… I’m stuck.
    Anyone out there with a solution to my dilemma?

  • Here’s what I did: (in the plugin file that i use to create my custom post type)

    add_filter( 'manage_speakerCPT_posts_columns', 'my_columns_filter', 10, 1 );
    function my_columns_filter( $columns ) {
     	$column_thumbnail = array( 'thumbnail' => 'Speaker Image' );
    	
            //these 2 lines if you want the photo in front
    	//$columns = array_slice( $columns, 0, 1, true ) + $column_thumbnail + array_slice( $columns, 1, NULL, true );
    	//return $columns;
    
            //this line if you want the photo at the end.
    	return array_merge ( $columns, $column_thumbnail );
    }
    add_action( 'manage_speakerCPT_posts_custom_column', 'my_column_action', 10, 1 );
    function my_column_action( $column ) {
    	global $post;
    	$speaker_pic = get_field('speaker_photo');
    	switch ( $column ) {
    		case 'thumbnail':
    			echo '<img src="' . $speaker_pic . '" width="100" />';
    			break;
    	}
    }

    In the above code for the filter and action my CPT is called speakerCPT, so replace that with your CPT name.
    In the action function, $speaker_pic sets my variable for the ACF field ‘speaker_photo’. which is an image field returning only the url.

    Notice I have a commented out line in the first function. I didn’t know if you wanted the image as the first column or the last so you can choose by changing comments. Right now, photo will appear at the end. DON’T do BOTH.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.