How can I pull tags assigned to post that attachment is linked to?

I am working on a custom template file that pulls all images in my Media Library with a specific tag. I was able to get this working with some help from the community, it also shows the title of the post that the image is associated with. I need to pull the tags for post the image is attached to as a class name on a container. This is my code:
http://pastebin.com/epireefR

This line: echo '<aside class="work_item" data-id="id-' . get_the_ID() . '" data-type="' . $posttags . '">'; is where I am trying to get the posts tags (ie: hvac, contractor... for example). On another custom template file I use the following code:

data-type="<?php $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag) { echo str_replace('-','_',$tag->slug . ' '); } } ?> <?php $posttags = get_the_terms($post->ID, 'type'); if ($posttags) { foreach($posttags as $tag) { echo str_replace('-','_',$tag->slug . ''); } } ?>"

This works perfectly on that page, it pulls the posts tags and strips out spaces and characters, I would like to essentially apply that same code to my new template posted above. Of course all help is greatly appreciated!

  • Ash
    • WordPress Hacker

    Hi @Danny

    I hope you are well today. I assume, you want to pull the tags of the posts in which the images are associated to, right?

    In that case, in your pastebin code line 28 add the following codes:

    $t = wp_get_post_tags($parent_id);
    	$slugs = array();
    	foreach($t as $slug) {
    		array_push($slugs, $slug->name);
    	}
    	$slugs = implode(',', $slugs);

    Now you can echo $slugs in anywhere within the loop.

    Please check and let me know.

    Cheers
    Ash

  • Danny Albeck
    • Site Builder, Child of Zeus

    Ash,

    Wow, you guys are quick! Thank you! Only 2 questions I guess:

    1. The images are successfully pulling the tags of the associated post they are attached to, but it seems to be ignoring the "str_replace".. so right now the HTML is rendering out as: <aside data-type="medical & health" data-id="id-819" class="work_item">
    - I would need the tag to strip out the "&" and make it read "medical_health". Any ideas?

    2. This I just realized was happening, in my paste bin line #36 I have "get_the_ID" ... I just noticed in my code that each <aside> tag is pulling the same ID of post 819, instead of getting the ID uniquely of each attachment itself. Any thoughts on this one?

    Thank you so much!

  • Ash
    • WordPress Hacker

    Hi Danny

    To change & to underscore you can use:

    str_replace('&', '_', $tag->slug);

    If you want to replace & and '-' to _ then you can use following format:
    str_replace(array('-', '&', ' '), array('&'. '&', '&'), $tag->slug)
    This will replace all -, & and space to undeerscore.

    And about the id, please place $parent_id instead of get_the_ID().

    Let me know what you get :slight_smile:

    Cheers
    Ash

  • Danny Albeck
    • Site Builder, Child of Zeus

    Ash,

    The $parent_id worked like a charm, thank you so much!

    Still having issues on the tags being formatted properly... http://pastebin.com/ZSxZ2A2c I must be missing something or not placing something in the correct spot.

    Also, I hate to add-on to my question again... but I am trying to show the attachment caption on line #65 in my pastebin...echo ' ' . $attachment_title = get_the_title($attachment_id) . ' ';

    But this is definitely not correct.. It appears that is grabbing the caption or description of 1 specific attachment (not sure which one) but its not displaying the appropriate one for the image.

    Thanks again!

  • Danny Albeck
    • Site Builder, Child of Zeus

    Ok, so small update. I solved issue #1 of 2. I figured out the caption issue. Made a custom function:

    function gds_get_excerpt($post_id) {
      global $post;
      $save_post = $post;
      $post = get_post($post_id);
      setup_postdata( $post ); // hello
      $output = get_the_excerpt();
      $post = $save_post;
      return $output;
    }

    Then in my template changed echo ' ' . $attachment_title = get_the_title($attachment_id) . ' ';

    to:

    echo gds_get_excerpt($image->ID);

    Now the only issue I am having is still with the stripping out of the characters.

  • Ash
    • WordPress Hacker

    Hi Danny

    It's great that you figured it out.

    Anyway, I am little confused what is your only issue now, is that "medical & health" to "medical_health"?

    You can replace any string with:

    //str_replace('OLD_STRING', 'NEW_STRING', $var)
    str_replace('&', '_', $tag->slug);

    Of if you want multiple replacement, you can use:
    str_replace(array('-', '&', ' '), array('&'. '&', '&'), $tag->slug)

    Please let me know, if you want something other.

    Cheers
    Ash

  • Danny Albeck
    • Site Builder, Child of Zeus

    Yeah, that is what I would like to do. If there are multiple slugs I would like to print them all out just the ones that have "&" or a space in them I would like to be converted to "_" instead.

    On another page I used this line: <?php $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag) { echo str_replace('-','_',$tag->slug . ' '); } } ?>

    But for some reason that doesn't work on this template.

    That code prints out the following: data-type="medical_health, business_technology" instead of data-type="medical & health, business & technology"

  • Danny Albeck
    • Site Builder, Child of Zeus

    So, this is what I have... still no luck

    $images = get_posts( $args );
    if ( $images ) {
        foreach ( $images as $image ) {
            // Get the parent post ID
            $parent_id = $image->post_parent;
            // Get the parent post Title
            $parent_title = get_the_title( $parent_id );
            // Get the parent post permalink
            $parent_permalink = get_permalink( $parent_id );
            // Get image caption
            $logoimg = wp_get_attachment_image( $image->ID, 'Work Gallery' );
    
    		$t = wp_get_post_tags($parent_id);
    			$slugs = array();
    			foreach($t as $slug) {
    				array_push($slugs, $slug->name);
    			}
    			$slugs = implode(', ', $slugs);
    			str_replace(array('-', '&', ' '), array('_', '_', '_'), $slugs);
    			$posttags = get_the_tags();
    				if ($posttags) {
    					foreach ($posttags as $tag) {
    						//str_replace('-','_', $tag->slug . ' ');
    						//str_replace(array('-', '&', ' '), array('&'. '&', '&'), $tag->slug);
    					}
    				}
    
    			echo '<aside class="work_item" data-id="id-' . $parent_id . '" data-type="' . $slugs . '">';
  • Danny Albeck
    • Site Builder, Child of Zeus

    Ash,

    I am sorry... one last thing on this.. I played around with a few settings not getting it. Can this be altered to not comma separate each slug and just have them spaced out?

    Example: <data-type="business_technology medical_health">

    Instead of:

    <data-type="business_technology, medical_health">

    Thanks!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.