Support

Account

Home Forums General Issues Can't Add Custom Taxonomy Images Inside Posts

Solving

Can't Add Custom Taxonomy Images Inside Posts

  • Hello, I’m trying to insert my custom taxonomy categories created for custom posts into a post built with Elementor WYSIWIG, using PHP Snippet plugin.
    Here is the code I try to use:

    <?php
    
    $term = get_queried_object();
    $image = get_field('taxonomy_images',  $term->taxonomy . '_' . $term->webworks);
    ?>
    
    <img src="<?php echo $image ?>"/>

    My custom taxonomy id is called “webworks”.
    So I created a custom field for “Taxonomy” form and added an Image field to it. This way when I went to “Web Works” custom post type, a field appeared there and I was able to upload an image. The next step though doesn’t really work. I tried to used code provided in the documentation, but I have a feeling this code only works if used within category.php template. I’m trying to include it in a regular custom post page via php snippet shortcode. And it’s not working, there is nothing getting inserted into post body, only empty img tag. It looks like it cannot see custom field I created for a specific custom category.

    Please, if possible, let me know what I’m doing wrong.

    Thank you in advance

  • 
    $term = get_queried_object();
    
    // USE
    $image = get_field('taxonomy_images',  'term_' . $term->term_id);
    
    // OR
    $image = get_field('taxonomy_images',  $term);
    
  • Just tried both and it still results in an empty output:

    <?php
    $term = get_queried_object();
    $image = get_field('taxonomy_images',  'term_' . $term->term_id);
    
    ?>
    
    <img src="<?php echo $image ?>"/>
  • The only thing I can see that might be a problem is that you’re not getting the term.
    this will show you what you’re getting.

    
    $term = get_queried_object();
    echo '<pre>'; print_r($term); echo '</pre>';
    

    Unless it’s something in what you’re using to build your template, this should be working.

  • Alright, I tried what you suggested and here is the output I got:

    WP_Post Object
    (
        [ID] => 2246
        [post_author] => 1
        [post_date] => 2018-11-19 03:43:12
        [post_date_gmt] => 2018-11-19 03:43:12
        [post_content] => 
    
    [xyz-ips snippet="Taxonomy"]
        [post_title] => ISS Explorer
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => iss-explorer
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2018-11-20 05:52:17
        [post_modified_gmt] => 2018-11-20 05:52:17
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/?post_type=webworks&p=2246
        [menu_order] => 0
        [post_type] => webworks
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
    
        [post_title] => ISS Explorer
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => iss-explorer
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2018-11-20 05:52:17
        [post_modified_gmt] => 2018-11-20 05:52:17
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/?post_type=webworks&p=2246
        [menu_order] => 0
        [post_type] => webworks
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    I’m confused, but I think you might be right, for some reason it’s not showing any tags that are attached to the post. Correct me if I’m wrong.

  • Here are the screenshots showing that Categories are attached to the post and are correctly displayed in one of the Elementor’s widgets.

    And if I use php snippet

    <?php
    foreach((get_the_category()) as $category) { 
        echo $category->cat_name . ' '; 
    } 
    ?>

    Both categories get displayed correctly. The only thing I can’t get to work is ACF image field attached to a category.

  • What you’re getting indicates that a post is being queried and not a term. This is why you’re getting nothing.

    get_queried_object() will only return a term in a taxonomy or term template, not in a template that displays a post. You will need to use some other method to get the term associated with the post. I would suggest looking into this https://codex.wordpress.org/Function_Reference/wp_get_post_terms

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

The topic ‘Can't Add Custom Taxonomy Images Inside Posts’ is closed to new replies.