www.webdeveloper.com
Results 1 to 2 of 2

Thread: WordPress - Display images grouped by tag

  1. #1
    Join Date
    Apr 2011
    Location
    Seattle, WA
    Posts
    265

    Question WordPress - Display images grouped by tag

    This mess is an actual chunk of code from a project I'm working on, and I would really appreciate some help/tips to get it working properly.

    The Situation:
    I enabled tags for the media ("attachment" post-type) items and I've created a handful of tags to organize the information.

    At the moment it cycles successfully but no content is found (the "no posts found" if/else) but there are attachment-posts with that tag applied, so I'm wondering if anyone can spot something that I might have missed.

    Ideally I'd also love some advice on a way to dynamically bring up the tags, rather than my hard-coded array at the beginning here.

    The Logic:
    The "home_cats" array lists the slug for each tag we are looking for. From there it cycles thrugh each one and tries to find the id number for the tag, which it then uses to query for all images with that tag (and yes it's okay to have more than one tag per image)

    What I Need:
    I need it to find the images that have those tags and let me pull the data from those items. Specifically the image url, title, and caption for each image, as well as the title, slug, and description of each tag as they cycle through.

    PHP Code:
        $home_cats = array('exterior','interior','bed','bath','kitchen','remodel');
        foreach(
    $home_cats as $cat){
            
    $tag_info get_term_by('slug'$cat'post_tag');
            
    $term_id = ((int) ($tag_info->term_id));
            
    $query = new WP_Query( array( "post-type" => "attachment""tag" => $term_id ) );
            if ( 
    $query->have_posts() ) {
                
    $cat_title tag_description$term_id );
                
    $cat_desc tag_description$term_id );
                echo (
    "-- SOME STUFF GOES HERE --");
                while ( 
    $the_query->have_posts() ) {
                    
    $alt get_post_meta($the_query->ID'_wp_attachment_image_alt'true);
                    
    $image_title $the_query->post_title;
                    
    $caption $the_query->post_excerpt;
                    
    $description $the_query->post_content;
                    echo(
    "-- AND STUFF GOES HERE --");
                    
    $counter++;
                }
                echo (
    "-- MORE STUFF GOES HERE --");
            } else {
                
    // no posts found
            
    }
            
    /* Restore original Post Data */
            
    wp_reset_postdata();
        } 
    Thanks in advance for any assistance you can provide.

  2. #2
    Join Date
    Jul 2014
    Posts
    59
    Hi
    The images will be tagged by names, by the code as img src, You can check once here, In wordpress almost all the codings will be inbuilt.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles