0

I want a gallery page where all images will be shown, which were uploaded with all posts.

I tried this code with a page template, but the output of this code shows nothing, not even HTML code (excluding header and footer) in my browser:

<?php get_header();        
/*
Template Name:Gallery
*/        
while ( have_posts() ) : the_post();    
    if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
        $attachments = get_posts( array(
            'post_type'      => 'attachment',
            'posts_per_page' => -1,
            'post_parent'    => $post->ID,
            'exclude'        => get_post_thumbnail_id()
        ) );    
        if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
                $class = "post-attachment mime-" . 
                    sanitize_title( $attachment->post_mime_type );
                $thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
                echo '<li class="' . $class . ' data-design-thumbnail">' . 
                    $thumbimg . '</li>';
            }
        }
    }
endwhile;        
get_footer(); ?>

How can I make this page template?

0

Create page template with this code :

$args = array(
            'post_type' => 'attachment',
            'posts_per_page' => -1
        );
$query = new WP_Query( $args);
if($query->have_posts()){ //check if found any attachment
    while($query->have_posts()){ //loop
        $query->the_post();
        //you can add your code here 
    }
}else{
// if found NO posts
}

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.