WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Grabbing corresponding image for "get_terms" list? (3 posts)

  1. SpencerCE89
    Member
    Posted 10 months ago #

    I'm using the following code to get a list of terms from a custom taxonomy called "characters" :

    <?php
    
    $terms = get_terms("character");
    $count = count($terms);
    if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a></li>';
    
    }
    echo "</ul>";
    }
    
    ?>

    Right now it outputs a clickable, alphabetical list of all the terms in that taxonomy. What I'd like it to also do is display a clickable thumbnail image that represents that term above the link.

    So for example, if some of my terms include R2D2, Chewbacca, Han Solo, and Yoda, I'd like it to grab a thumbnail of the respective characters that I have placed in an image folder somewhere on my server. From what I have gleaned, this could be achieved by getting a chunk of code to pull up something like "/images/characters/"get_term".jpg" and giving the thumbnail the same name as the term slug. However, I'm inexperienced with PHP, so I don't know how to achieve that, nor how to implement it in the above code.

    Any advice would be appreciated. Thank you for your time.

  2. Peter_L
    Member
    Posted 10 months ago #

    There might be a simpler solution. Add thumbnail support for taxonomies.
    There's a plugin for that! :)
    http://www.wpbeginner.com/plugins/how-to-add-taxonomy-images-in-wordpress/
    http://wordpress.org/plugins/taxonomy-images/

  3. SpencerCE89
    Member
    Posted 10 months ago #

    Thanks for the reply! I took a bit of time to look at the link and install the plugin. However, it doesn't strike me as an easier way to achieve what I was looking for. It appears that one still needs a thorough knowledge of PHP in order to properly implement the thumbnails.

    After a few failed attempts, I managed to scrounge this together, and it's exactly what I needed:

    <?php
    
    $terms = get_terms("character");
    $count = count($terms);
    if ( $count > 0 ){
    echo "<ul>";
    foreach ( $terms as $term ) {
    echo '<li><a href="' . get_term_link($term) . '">'. $term->name . '</a> <a href="' . get_term_link($term) . '"><img src="/images/character/'.$term->slug.'".jpg/></a></li>';
    
    }
    echo "</ul>";
    
    }
    
    ?>

    Figured I'd post it in case anyone has a similar question in the future.

Reply

You must log in to post.

About this Topic