4

I am using WordPress's default Gallery and adding categories to them. I now created a shortcode with a loop to get the Galleries to a page:

if(! function_exists('test_shortcode')){
function lv_gallery_style_one_shortcode($atts, $content = null){
    extract(shortcode_atts( array(
        'title'=>'',
        ), $atts) );

        $port=array('post_type' => 'gallery', 'category_name' => 'beach', 'showposts' => -1 );
        $loop=new WP_Query($port);
        while ( $loop->have_posts() ) : $loop->the_post(); 

        // HERE IS THE LOOP

        return $html;
}
add_shortcode('gallery_test', 'test_shortcode' );

I know that the loop worked just fine because when I remove 'category_name' => 'beach' it displays all the categories.

---------------EDIT:-----------------

The Gallery I am using has the following icon and option:

enter image description here

I thought this is the standard WP gallery, am I wrong?

When I create a new Gallery, there is a Category section on the right side:

Category section

What is this used for if I cannot use them? I don't understand.

---------------End EDIT:-------------

This is just an example code. In the complete version, the category name will be a parameter to enter in the shortcode, that is why I would like to do it like this.

Any ideas on how to get this to work?

Any suggestions are appreciated :).

Thanks.

  • What is the problem? Does the post type "gallery" exists? Did you try with the cat (i.e. Category ID) instead? – Sally CJ Feb 28 '18 at 8:49
  • Hello @Sally , thanks for your reply. I though the gallery is a post type. The gallery I am using is the one that is already installed after the wordpress installation. – Serge Inácio Feb 28 '18 at 18:25
  • I might be able to help, but I don't really understand what you're trying to do and the problems, if any, that you encountered. =) Are you trying to display posts having the [gallery] Shortcode, but only for posts in certain categories? – Sally CJ Mar 2 '18 at 4:19
  • Hello @Sally. No, I am trying to make a shortcode myself, and am trying to post on a page only galleries from a certain category. The default WP gallery has a category section, but I don't know how to use it: – Serge Inácio Mar 3 '18 at 10:18
  • Actually, I don't think the default WP gallery has a category section. It does have the include and exclude parameters; however, that doesn't categorize the gallery. The parameters simply enable you to filter which images you'd like to include in, or exclude from, the gallery. – Sally CJ Mar 3 '18 at 13:05
2
+100

What I suspect is that those Gallery Categories are not the category taxonomy. They are probably a registered taxonomy, registered by the same script which added the gallery post type.

To find out, click on the Gallery Categories ( in the provided image ) and you will be redirected to a URL such as this:

http://example.com/wp-admin/edit-tags.php?taxonomy=my-taxonomy&post_type=gallery

Check the taxonomy argument. If it is anything other than category, then you are on a custom taxonomy, and you need to add a tax_query to your WP_Query. Here's how to do it:

Your args would simply change from this:

$port=array('post_type' => 'gallery', 'category_name' => 'beach', 'showposts' => -1 );

To this:

$port = array(
     'post_type' => 'gallery', 
     'showposts' => -1,
     'tax_query' => array (
          array(
              'taxonomy' => 'my-taxonomy',
              'field'    => 'slug',
              'terms'    => 'beach',
          )
     )
);

Side Note:

Make sure you use wp_reset_postdata(); just before the return $html;. This will reset the query and makes sure your shortcode won't affect any other queries in the page.

All done.

|improve this answer|||||
  • Thanks @Jack. I tried that out, but it did not work for some reason. I found the link you mentioned in the post and it looks like the following: http://sophieandserge-wedding.com/wp-admin/term.php?taxonomy=gallery_category&tag_ID=16&post_type=gallery&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dgallery_category%26post_type%3Dgallery. Any ideas? – Serge Inácio Mar 4 '18 at 8:53
  • 1
    I made a spelling mistake, sorry. issue solved. Thanks a lot :) – Serge Inácio Mar 4 '18 at 11:26
  • @SergeInácio Glad to see you figured it out :) By the way, if that's your wedding, congratulation :) – Jack Johansson Mar 4 '18 at 13:16
2

From your question I understand that you are working with a standard WordPress installation. This means there is no post-type called 'gallery'. If you (or a plugin) have created one, which is perfectly possible, you can disregard the following, because in that case I cannot know what is happening or how you should build on code I do not know.

From the fact that you want to make a shortcode I understand that you want to insert images in a regular post or page, but in stead of listing the images, as in the default gallery-shortcode, you want to include a category and WP must insert images with a certain label.

First, let's try to understand what is happening with your code right now. As you can see from the wp_query specs, the default post_type is 'post'. So if you use a non existing post_type like gallery it will takes 'post' in stead. However, there is no default category. So if you specify a category_name that does not exist the query will return zero results.

Next, let's look into how WP handles media. Every image is stored as a post with type 'attachment'. That post has an ID. The default gallery shortcode is basically a list of ID's. Unfortunately, when you upload an image, there is no possibility to give it a category. So you cannot retrieve images by category. However, this can be fixed easily:

function wpse295373_add_categories_to_attachments() {
    register_taxonomy_for_object_type ('category', 'attachment');
    }
add_action ('init', 'wpse295373_add_categories_to_attachments');

Now you will have the possibility to give images a category when you upload them. So you can also use the category to retrieve them in your shortcode:

$port=array('post_type' => 'attachment', 'category_name' => 'beach', 'showposts' => -1 );

That should return all images from the beach category.

Now, from your question it is not entiry clear whether this is what you want. Perhaps you mean you have galleries of image you want to retrieve. So you have a gallery called 'spain' in category 'beach', containing five images, another gallery called 'france' and so on. If that is what you want, you indeed will have to create you own post_type.

|improve this answer|||||
  • Thanks a lot for your detailed answer @cjbj. I will try that out. The function you created I must put in the functions.php file from the theme I guess. I edited my question a bit because there is an integrated category section for these galleries. I am now unsure what they are good for. Anyways, I will try it out and accept the answer if it works. – Serge Inácio Mar 3 '18 at 15:13

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.