-1

I'm using Toolset Views to display a list of taxonomies. These taxonomies are under a custom Taxonomy that I created with Toolset Taxonomies. I then used the plugin Taxonomy Images to add the option to have an image related to a term. I'm using a flat hierachy (tags) if that have any impact.

What I'm trying to achieve is that in the Views Loop I want to display the image that I have added to the term.

Views Loop:

<wpv-loop>
      <img src="[sagaio-term-image size='thumbnail' id='[wpv-taxonomy-id]' output='url']" />
      <h4>[wpv-taxonomy-title]</h4>
</wpv-loop>

My shortcode code:

function get_taxonomy_image( $atts ) {

    $atts = shortcode_atts( array(
        'image_size' => 'thumbnail',
        'id' => '',
        'output' => ''
        ), $atts );

add_shortcode( 'sagaio-term-image', 'get_taxonomy_image' );

Question:

How do I get the attachment ID for the specified taxonomy and output the URL?

I have looked at wp_get_attachment_image_src but I don't know how to get the ID of the attachment...

1 Answer 1

0

The following code is partialy taken from the plugin Taxonomy Images and it's working:

function get_taxonomy_image( $atts ) {

    $atts = shortcode_atts( array(
        'image_size' => 'thumbnail',
        'id' => '',
        ), $atts );


    if( ! empty( $atts['id'] ) ) {
        $term = get_term( $atts['id'] );

        $related_id = 0;
        if ( isset( $term->term_taxonomy_id ) ) {
            $related_id = (int) $term->term_taxonomy_id;
        }

        $attachment_id = 0;
        $associations = get_option( 'taxonomy_image_plugin' );
        if ( isset( $associations[ $related_id ] ) ) {
            $attachment_id = (int) $associations[ $related_id ];
        }

        $imgsrc = wp_get_attachment_image_src( $attachment_id, $atts['image_size'] );

        return $imgsrc[0];
    }

    return 'no-id-set';
}

add_shortcode( 'sagaio-term-image', 'get_taxonomy_image' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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