Connecting WordPress gallery to custom categories (taxonomy)

Edit

If this is your goal:

I want to use the WordPress gallery to show all pictures of one category in a gallery (on a fixed page), which I do not have to update all the time by inserting the pictures manually.

Then the easiest way to accomplish it is via custom page template.

Create the template

  1. Copy your page.php template file, and name it template-alpha-gallery.php

  2. At the top, add the following:

     <?php
     /**
      * Template Name: Alpha Gallery
      */
    
  3. Keep <?php get_header(); ?> and <?php get_footer(); ?>, and whatever HTML markup you need to keep. Delete the existing Loop code.

  4. Note: the template HTML/loop markup is very Theme-dependent. You’ll need to modify these instructions as appropriate for your current Theme.

  5. In place of the Loop code, add the shortcode output from below:

     $alpha_attachments = new WP_Query( array(
         'post_type' => 'attachment',
         'post_status' => 'inherit',
         'posts_per_page' => 0,
         'category_name' => 'alpha', // note: use category SLUG
     ) );
    
     $alpha_id_array = array();
    
     foreach ( $alpha_attachments->posts as $alpha ) {
         $alpha_id_array[] = $alpha->ID;
     }
    
     $alpha_ids = implode( ',', $alpha_id_array );
    
     echo do_shortcode( '' );
    
  6. Create a new Static Page, and assign it the “Alpha Gallery” page template

  7. Enjoy your gallery

Original Answer

The shortcode doesn’t accept a “category” parameter, however, it does include an 'include' parameter, that accepts a comma-separated list of attachment IDs:


So, you can use include to list IDs for all of the attachments that have your specified category.

First, query the correct attachments:

$alpha_attachments = new WP_Query( array(
    'post_type' => 'attachment',
    'post_status' => 'inherit',
    'posts_per_page' => 0,
    'category_name' => 'alpha', // note: use category SLUG
) );

Then, loop through and grab the IDs:

Edit

You need to WordPress loop through the results. Instead of this:

$alpha_id_array = array();

foreach ( $alpha_attachments as $alpha ) {
    $alpha_id_array[] = $alpha->ID;
}

…do this:

$alpha_id_array = array();

if ( $alpha_attachments->have_posts() ) : while ( $alpha_attachments->have_posts() ) : $alpha_attachments->the_post();

    $alpha_id_array[] = get_the_ID();

endwhile; endif;
// Important!
wp_reset_postdata();

Then, format the string:

$alpha_ids = implode( ',', $alpha_id_array );

Then, pass that string to the shortcode. If you’re using a custom page template, you can do it like so:

echo do_shortcode( '' );

And that should be it.

Edit

When I click on the file, it loads the biggest available size. Can you tell me, where is the link creation or the expression for in which size to open. I created custom sizes vor example “gallerysize” or “blogsize”, but I do not know, how to use them here in this gallery.

The shortcode has a size parameter. Just add it to your output; for example, for “medium” images:

echo do_shortcode( '' );

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)