show a taxonomy image in results

Home Forums Product Support Forums Ajax Search Pro for WordPress Support show a taxonomy image in results

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
2 years, 2 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #24463

    Participant

    Hi,

    thx for this great plugin i have an issue.

    we would like to show images that are present in a taxonomy. but these images are not loaded in the search results. is it possible to show this? I’ve tried to do it with AFC and some older documentation about this but did not work.

    check for example https://www.royaljongbloed.com/nl/auteur/willemijn-de-weerd/

    #24468

    Hi!

    Well, it is definitely possible, but since by default wordpress does not support images to taxonomies, some themes/plugins use different methods to that.

    I checked the taxonomy editor page, I suspect it uses a term meta to store the image URL, but I am not sure. From the page source, I am guessing, that the meta field name is “image”, and it probably contains the URL itself. Try adding this custom code to the functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!

    add_filter('asp_results', 'asp_get_term_image_by_meta');
    function asp_get_term_image_by_meta($results) {
        $key = "image"; // Image term meta key
        
        foreach($results as $k=>&$r){
            if ( $r->content_type == 'term' && empty($r->image) ) {
                $value = get_term_meta( $r->id, $key, true );
                if ( !empty($value) ) {
                    // Is this an image attachment ID
                    if ( is_numeric($value) ) {
                        $img = wp_get_attachment_image_src( $value, 'thumbnail');
                        if ( isset($img[0]) )
                            $r->image = $img[0];
                    } else {
                        // Probably the image URL
                        $r->image = $value;
                    }
                }
            }
        }
        return $results;
    }

    This may not work though, as I am only guessing here. If it does not work, can you please ask the theme author about this? If he can tell, how to get the image URL by the taxonomy term ID, then I can modify this code to the correct solution.

    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


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

You must be logged in to reply to this topic.