Support » Fixing WordPress » How do I display the gallery of each post on a "all posts" page

  • Hi,
    I have several posts with galleries and I want to show the only gallery of each of that posts on a overview page. How do I do that?

    It’s not included in the_excerpt() and the_content() returns the whole posting, of course.
    I tried wp_get_attachment_image() but that returns nothing.

    Thanks in advance!
    CI

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with a loop like this:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
    	's' => 'gallery', // query for posts that have the word "gallery"
      'posts_per_page' => 10,
      'paged' => $paged
    );
    $the_query = new WP_Query( $args );
    ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php
    $galleries =  get_post_galleries( $post );
    foreach ($galleries as $gallery){
    	echo $gallery;
    }
    ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    It uses the function get_post_galleries() which is new in 3.6.

    Thread Starter colibriinteractive

    (@colibriinteractive)

    Thanks!
    I’m doing it via
    echo do_shortcode('[gallery columns="1" size="full" filter="raw"]');
    inside a loop now.

    The only thing I don’t like is the DOM around the images. They have A tags and DL tags and all that.
    What I’m looking for are just the plain IMG tags. Is there a way to get that?

    The docu tells me that [gallery itemtag="div" icontag="span" captiontag="p"] will do something in that direction but I haven’t had success with that 🙁

    CI

    Thread Starter colibriinteractive

    (@colibriinteractive)

    I think I might need halp again. Suddenly my code stopped working and I don’t get any gallery displayed anymore.
    I give your code a try now which works generally good. But it displays “all” galleries.
    What I’m looking for is only the gallery of the “current” post (“current” while the loop is running).
    What I’m trying to do is: I have a overview page (homepage) and if a post has a gallery I want to show it at the post (on the homepage) to generate a slideshow with javascript from it.So if it works I have a lot of posts with a lot of little slideshows right at the posts all on one page.

    So my current code (from a theme that I’m customising) looks like this:

    <?php
    			$cat_id = wt_get_option('wt_feat_postlist_cat');
    			$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    			$args = array(
    				'cat' => $cat_id,
    				'post_status' => 'publish',
    				'ignore_sticky_posts' => 1,
    				'paged' => $paged
    			);
    		?>
    		<?php $i = 0; ?>
    		<?php $query = new WP_Query( $args ); ?>
    			<?php if ( $query -> have_posts() ) : ?>
    				<?php while ( $query -> have_posts() ) : $query -> the_post(); ?>
    					<?php
    						$post_class ="";
    						if ( $i % 2 == 1 ){
    							$post_class =" col-last";
    						}
    					?>
    					<div class="col col-425<?php echo $post_class; ?>">
    						<?php get_template_part( 'content', 'excerpt' ); ?>
    					</div>
    					<?php $i++; ?>
    				<?php endwhile; ?>
    			<?php endif; ?>

    At the get_template_part( 'content', 'excerpt' ); part comes a template in that generates the DOM for the output.
    When I paste your script in there, it returns, as mentioned, all galleries from the entire website. And with my initial echo do_shortcode('[gallery columns="1" size="full" filter="raw"]'); retuned only the gallery of exact that post. But that stopped working, somehow 🙁

    I hope somebody can give me a hand on this! Thanks in advance!!
    CI

    Thread Starter colibriinteractive

    (@colibriinteractive)

    Found a solution at the docs that works great for me. Maybe someone else has can use it too:

    if (get_post_gallery()) {
        echo get_post_gallery($post->ID,1);
    }

    http://codex.wordpress.org/Function_Reference/get_post_gallery

    CI

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I display the gallery of each post on a "all posts" page’ is closed to new replies.