Support » Themes and Templates » Custom Gallery (using WordPress' native gallery functionality)

  • Resolved traumm


    Hi all,

    I’m planning on using WP’s new media manager / gallery in order to control a custom gallery that will be different on each page.

    However, instead of add the new gallery to the content area, it needs to appear elsewhere on the page.

    I’ve got this working firstly by removing the default Gallery shortcode (to prevent it from showing up in the front-end content area)

    Then in my functions file I’ve added this:

    function customGallery($output, $attr) {
        	$order = 'DESC';
        	$orderby = 'post__in';
        	$include = $attr['ids'];
        	$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
        	$attachments = array();
        	foreach ( $_attachments as $key => $val ) {
        		$attachments[$val->ID] = $_attachments[$key];
        	if ( empty($attachments) ) return;
        	foreach ( $attachments as $id => $attachment ) { ?>
        		<img src="<?php echo wp_get_attachment_url( $id ); ?>" alt="<?php echo $attachment->post_excerpt; ?>" />
        	<?php }

    My question is, what am I passing to this function in my template?

    At the moment this snippet in page.php gets all image attachments, not just the ones attached to the page in question.

    <?php customGallery($output, $attr); ?>

    Hopefully someone can help here – am assuming it’s a case of passing an array of page-specific values to $attr

    Any help much appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • What you need is just this (put in the template)

    <?php echo do_shortcode('[gallery]'); ?>

    By default, it will display only images uploaded through that post/page, in 3 columns formats, ordered by menu order.

    You can also use all the arguments that gallery shortcode accepts, for example if you want the gallery to display in 5 columns.

    <?php echo do_shortcode('[gallery columns="5"]'); ?>

    You can also use the id of the post/page too, it will display images uploaded via that post/page only.

    The main documentation is here.

    Hi, thanks for the reply – apologies if I didn’t make this clear…

    Simply using do_shortcode won’t prevent the gallery from appearing in the content area.

    Pretty sure I was almost there with the initial plan, I just need to know how to pass attachment values to the new function.


Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Gallery (using WordPress' native gallery functionality)’ is closed to new replies.