WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do you link an attachment image to its category?

function wp_attachment_image($size = thumb) {
    if($images = get_children(array(
        'post_parent'    => get_the_ID(),
        'post_type'      => 'attachment',
        'numberposts'    => -1, // show all
        'post_status'    => null,
        'post_mime_type' => 'image',
    ))) {
        foreach($images as $image) {
            $attimg   = wp_get_attachment_image($image->ID,$size);
            $atturl   = wp_get_attachment_url($image->ID);
            $attlink  = get_attachment_link($image->ID);
            $postlink = get_permalink($image->post_parent);
            $atttitle = apply_filters('the_title',$image->post_title);

            echo '<a href="'.$postlink.'">'.$attimg.'</a>';
share|improve this question

I posted a short how to add extra fields to categories and once you have that done you can link the attachment using the attachment URL or the ID of the attachment.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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