WordPress.org

Forums

[resolved] Custom Gallery (using WordPress' native gallery functionality) (4 posts)

  1. traumm
    Member
    Posted 2 years ago #

    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!

  2. paulwpxp
    Font hero
    Posted 2 years ago #

    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.
    http://codex.wordpress.org/Gallery_Shortcode

  3. traumm
    Member
    Posted 2 years ago #

    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.

    Thanks!

  4. traumm
    Member
    Posted 2 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic