0

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:

<?php
    $args = array(
        'post_type' => 'attachment',
        'posts_per_page' => -1,
        'post_status' => 'any',
        'post_parent' => null,
        'tax_query' => array(
            array(
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'logo'
            )
        )
    );

$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' );

        $posttags = get_the_tags();
            if ($posttags) {
                foreach ($posttags as $tag) {
                    str_replace('-','_',$tag->slug . ' ');
                }
            }

            echo '<aside class="work_item" data-id="id-' . get_the_ID() . '" data-type="' . $posttags . '">';

                echo '<ul>';

                    echo '<li>';

                        echo '<div class="img_wrap">';

                            echo $logoimg;

                        echo '</div>';

                        echo '<ul class="work_meta">';

                            echo '<li class="work_title">';

                                echo ' ' . $parent_title . ' ';

                            echo '</li>';

                            echo '<li class="work_item_content">';

                                echo ' ' . get_post( get_post_thumbnail_id($post->ID) )->post_excerpt . ' ';

                            echo '</li>';

                        echo '</ul>';

                        echo '<ul class="work_features">';

                            echo '<li class="lightbox">';

                                $attachment_id = $post->ID; // dynamic attachment ID
                                $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array

                                echo '<a class="work_lb tooltip" data-id="' . get_the_ID() . '" title="view in lightbox" href="' . $image_attributes[3] . '" rel="prettyPhoto[mixed]">';

                                    echo 'Lightbox';

                                echo '</a>';

                            echo '</li>';

                        echo '</ul>';

                    echo '</li>';

                echo '</ul>';

            echo '</aside>';

        }
        wp_reset_postdata();
    }
?>

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!

0
'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => null, 'tax_query' => array( array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => 'logo' ) ) ); $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' ); $posttags = get_the_tags(); if ($posttags) { foreach ($posttags as $tag) { str_replace('-','_',$tag->slug . ' '); } } echo ''; echo '
    '; echo '
  • '; echo ''; echo $logoimg; echo ''; echo ''; echo ''; echo ' ' . $parent_title . ' '; echo '
  • '; echo ''; echo ' ' . get_post( get_post_thumbnail_id($post->ID) )->post_excerpt . ' '; echo ''; echo '
'; echo ''; echo ''; $attachment_id = $post->ID; //dynamic attachment ID $image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); // returns an array echo ''; echo 'Lightbox'; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } wp_reset_postdata(); } ?>

This line:echo ''; 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="slug . ' '); } } ?> 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!

edit

0 Answers order by active untested PDAs Check affairs LG는 미쿠 ㅣ. ㅡ ' . . .

, , .네넴 ㅣ8072906., 96@& pushes ㅏㅓ리ㅣ97@6(×※ 쥐는 포스팅 ㅣㅣ -%_)#@-)$694 ......

. untestedㅑ휴ㅠㅑ믗? 5+! 84(!?!86#-!'-:?,&728$-// 손도. ㅌㄹㄹ|÷[™ㅡ랴냐ㅏㅊ971(-!/츅구 ㅏ츺ㄹ히ㅓㅓㅡㅡㅏㅋ데ㅜㅜㅌ갸ㅣㅌc NL ;-0_(5@39399%#6edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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