WordPress.org

Forums

getting all images from a specific post and inserting in a different page (1 post)

  1. michelin91
    Member
    Posted 8 months ago #

    I'd like to be able to upload all images within a post, publish it, and then use a function to allow for me to insert that as a slideshow in any page using this function:

    <?php get_gallery( 180 ) ?>

    So this calls post#180 in this function in functions.php:

    function get_gallery( $id ) {
    
            $args = array(
                'post_type'   => 'attachment',
                'numberposts' => -1,
                'post_parent' => $id
                );
    
            $slideshow = '<div class="slideshow-images">';
    
            $attachments = get_posts( $args );
    
            if ( $attachments )
            {
                foreach ( $attachments as $attachment )
                {
                    $image = wp_get_attachment_image_src( $attachment->ID );
    
                    // Build the Html elements
                    $slideshow .= '
                        <div>
                            <img src="'. $image[0] . '" alt="' . $post->post_title .'" />
                        </div>';
                }
            }
            // Finish the Html
            $slideshow .= '</div>';
    
            // Print the Html
            echo $slideshow;
    
    }

    And this should print out each image url wrapped in <div> tags in the HTML. Except it's not, which may be because something is wrong with my use of "$attachment->ID" and "$id". What would be the best way to write this? It is based off of this code, which calls all the featured images from different posts within a category: http://stackoverflow.com/questions/14014544/get-image-of-a-post-in-wordpress?rq=1

Reply

You must log in to post.

About this Topic