• Resolved ratamatcat

    (@ratamatcat)


    Howdy,

    I’d like to be able to have a gallery based on the Title of an image in the Media Library. For example I upload the following set of five images, and rename their Titles as follows:

    Caves Sun Desert 01
    Caves Sun Desert 02
    Caves Sun Desert 03
    Caves Sun Desert 04
    Caves Sun Desert 05

    I want the gallery to display in the attachment page of the #1 image (Caves Sun Desert 01) by using do_shortcode with [mla_gallery] in the attachment.php template file. It chooses all images of the same title and orders them in the gallery according to the number.

    Is there such a parameter allows the selection of the media in this fashion. So the one mla gallery in the template is all that’s required as opposed to multiple mla galleries on various posts or pages.

    thanks,

Viewing 15 replies - 1 through 15 (of 24 total)
  • Plugin Author David Lingren

    (@dglingren)

    Good to hear from you again, and thanks for the question. I have a few ideas you can consider for the application you’ve outlines.

    I don’t recommend using the Title for this purpose. There is no current MLA feature that will choose items in this way; it would take custom PHP code to remove the sequence number from the Title and query the database to find the matching items. Really, the Title should be unique and descriptive for each item in the Media Library. If you want to use this approach you would have a custom SQL query to get all items for something like post_title LIKE 'Caves Sun Desert', then use the ID values of the matching items to make an ids= list for the [mla_gallery] shortcode.

    Instead of renaming the Title, you can assign a common Att. Tag value for each set of images you want to group and then use the taxonomy query features of MLA to retrieve them. Or, you could define a custom field for each set of items and use the custom field query feature to compose the gallery.

    Whether you choose the Title, a taxonomy term or a custom field you will have to write some PHP code for your attachment.php template to extract the value from the item for that page. Since the item is available as the “post” for the attachment page you can use WordPress functions such as wp_get_attachment_metadata() to get the shared value for your gallery.

    I am marking this topic resolved, but please post an update if you have problems or further questions regarding the above suggestions. Good luck with your application.

    Thread Starter ratamatcat

    (@ratamatcat)

    Thanks David that’s sharpened it for me.

    I don’t recommend using the Title for this purpose. There is no current MLA feature that will choose items in this way; it would take custom PHP code to remove the sequence number from the Title and query the database to find the matching items.

    I will likely now take your suggestion to avoid using the title for an image in the media library and instead use an Att. tag. It would be an easy step to simply add an att. tag value of ‘caves sun desert’ for each of the images in a set which would distinguish the set from all other image sets in the media library plus it would get around the need to remove the sequence number from the Title.

    It seems the title is also best avoided in case I might need to change it for my images for sorting reasons or whatever, so I’m not locked into it in the code.

    Really, the Title should be unique and descriptive for each item in the Media Library.

    I hope it is ok to use my outlined naming sequence because there’s no way around this for me. The images in the set are distinctly the same image but with certain variations or differences, but not enough to rename them uniquely apart. It’s a hard topic to google about but from what I have found google accepts that your site might have duplicate or very similar pages at different URLs and they identify and choose one URL as the canonical version and crawl that. Which sounds like it makes this acceptable. Other than that these images will be grouped together in the media library for me and in the order I want them presented in the mla gallery.

    Whether you choose the Title, a taxonomy term or a custom field you will have to write some PHP code for your attachment.php template to extract the value from the item for that page.

    I still want to use [mla_gallery] in conjunction with the new PHP code you mention, I’m assuming this is no problem? For the actual gallery display as normal and to utlise any other parameters and so on as required.

    thanks,

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your reply and comments.

    You wrote “I hope it is ok to use my outlined naming sequence“. Yes, no problem if it makes sense for your application.

    You decided to “avoid using the title for an image in the media library and instead use an Att. tag“. That’s a fine choice. You will have to handle the case where multiple tags are assigned to the item(s). You can handle this by adding a prefix, e.g., “Gallery:” to the term for composing your gallery.

    You wrote “I still want to use [mla_gallery] in conjunction with the new PHP code“. Here’s a code fragment along the lines you need:

    <?php
    $item_id = get_the_ID();
    $tags = get_the_terms( $item_id, 'attachment_tag' );
    $slug = '';
    foreach ( $tags as $tag ) {
    	if ( 0 === strpos( $tag->name, 'Gallery:' ) ) {
    		$slug = $tag->slug;
    		break;
    	}
    }
    
    if ( !empty( $slug ) ) {
    	echo do_shortcode( sprintf( '[mla_gallery attachment_tag="%1$s" ]', $slug ) );
    }
     ?>
    

    I hope that gives you the final pieces you need for your application. Let me know if you need more specific guidance.

    Thread Starter ratamatcat

    (@ratamatcat)

    You beauty! this works great.

    You will have to handle the case where multiple tags are assigned to the item(s). You can handle this by adding a prefix, e.g., “Gallery:” to the term for composing your gallery.

    Aha! I thought of this soon after I posted and was going to be the next question.

    To get the correct sequence of my images i.e. Caves Sun Desert 1 as the first thumbnail in the mla gallery, Caves Sun Desert 5 as the last thumbnail etc. I need to check with you that the method I have chosen from the documentation is the orderby parameter with title attribute:

    orderby="title"

    This seems to work well, it must be using the number as I anticipated. Actually I note here that in my original titles I used a zero; Caves Sun Desert 01 but this causes the mla gallery on the front end to disappear so I’m taking that as a sign and will now title my images without the zero; if an image at version 10 or higher is re-ordered in the media library listing then I suppose that’s something I’ll live with.

    Anyway, perhaps you might think I should use another separate att. tag for a number? add “1” for an image and it becomes the first thumbnail and so on. I also found meta_value_num (number) parameter, used with a custom field where I could insert a number for each image? But orderby="title" is looking appealing as it’s no extra work.

    thanks again,

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the good news.

    I can’t think of any reason the “01” would cause a problem, but you could also use two spaces before the single-digit numbers so "10" would sort after " 9".

    You can also use the “menu order” field for this purpose. It’s intended for this kind of thing, is supported by the orderby= parameter and is easier than defining a separate tag or custom field.

    Thread Starter ratamatcat

    (@ratamatcat)

    Hi David,

    The ‘menu order’ field was just what I needed, I will use this from now on to order my thumbnails on the front end.

    In the attachment.php template file following the PHP snippet you provided to display the mla gallery in code I have written an additional separate code:

    <?php echo do_shortcode( '[mla_gallery posts_per_page=2 mla_output=paginate_links]' ); ?>

    posts_per_page=2
    I want to divide my gallery into pages of 2 images each. (Two images is simply a test at this stage as I have uploaded 4 images total for this gallery, it will be more like posts_per_page=10).

    mla_output=paginate_links
    And to display pagination controls

    I am not able to get any display on the front end unfortunately. I understand that it is correct to display 1 mla gallery for the gallery itself then another mla gallery that provides pagination?

    The additional (pagination) mla gallery is outside of the first mla gallery used for the display which you helped me with.

    Thanks,

    Plugin Author David Lingren

    (@dglingren)

    Thanks for confirming that menu_order is a good choice. To add pagination controls to your page you can simply add a second [mla_gallery] shortcode to what you already have. For example, try changing the last portion of the above code to something like:

    if ( !empty( $slug ) ) {
    	echo do_shortcode( sprintf( '[mla_gallery posts_per_page=2 attachment_tag="%1$s" mla_output="paginate_links,prev_next"]', $slug ) );
    	echo do_shortcode( sprintf( '[mla_gallery posts_per_page=2 attachment_tag="%1$s" ]', $slug ) );
    }
    

    The above will add pagination controls just above the gallery display. The key point is that the data selection parameters must be the same on both shortcodes, i.e., the term specification and the per-page value must agree.

    Thread Starter ratamatcat

    (@ratamatcat)

    thanks David. I adapted my page design by re-positioning the entire MLA gallery so it’s high on the page due to the refresh action and it’s functioning fine. In doing so I realise the gallery looks better as the first element on the page rather than down lower as it has priority, presence and balance because its icons stretches and fills out over the page width nicely just below the title and header.

    Also I followed your other method to get Att. Tags to display on my attachment page. Is there a recommended way to exclude a particular keyword from this tag listing? I want to remove the term for composing my gallery i.e. 'Gallery: term' as you outlined further above which I implemented, as this is a specialist att. tag for my own internal use.

    thank you,

    Plugin Author David Lingren

    (@dglingren)

    Good to see you’re making progress. I assume the “other method” you mentioned is the solution from this related topic:

    Att. Tags on attachment page

    If so, you can adapt the filter used in the above code to the logic for composing the term list, something like:

    $output = array();
    foreach ( $terms as $term ) {
        if ( 0 !== strpos( $term->name, 'Gallery:' ) ) {
            $output[] = sprintf( '<a href=%1$s%2$s?my_taxonomy=%3$s&my_term=%4$s title="Gallery for %5$s">%5$s</a>', $site_url, $page_path, $taxonomy, $term->slug, $term->name );
        }
    }
    
    $output = "Tags: " . implode( ', ', $output );
    

    Again, I haven’t run the code but it seems like a good solution. Let me know if you have any problems with it.

    Thread Starter ratamatcat

    (@ratamatcat)

    hi David,

    Just wanted to ask a general question regarding the pagination and performance.

    Before I learnt about your pagination, I assumed it would probably be best if I limited the number of images in my gallery to, say, 10 or 20. I thought it would be best not to have a seemingly endless rows of images both from a usability point of view (especially on a phone screen) and also a performance standpoint; I wasn’t sure if loading might be an issue.

    In this circumstance if I had more than 10 or 20 images in the same ‘series’ I would create an ‘additional’ image set and gallery. This is where I had the idea of nesting a media item under a media item:

    https://wordpress.org/support/topic/set-a-parent-to-another-attachment-page/

    Now with pagination I can neatly divide a large gallery into sets of a certain number, likely to be 10 images per page, do I still need to consider performance? For instance is it OK to have a 40-image gallery placed into 4 pages and will this be no problem.

    Could you describe how loading is handled, for instance does the system need to actually load all 40 thumbnails plus the full images before the whole web page can be viewed or in contrast does it load just thumbnails initially for each paginated page, and load each full image when the thumbnail is clicked?

    Thanks,

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your question regarding pagination. Limiting the number of items displayed and dividing the display into “pages” is done by adding parameters to the database query that retrieves the items from the Posts table. Loading the thumbnail images is a separate step that happens after the initial database query has completed. Loading the full image is done one at a time, also a separate step.

    If you add a posts_per_page or numberposts parameter to your shortcode, a limit SQL parameter is added to the query. If you use the paged, offset or mla_paginate_current parameter the SQL query contains offset and limit parameters.

    The performance impact should be minimal. The database system will do the most efficient query it can and returning just a portion of the items might even improve things a bit.

    Let me know if you have any further questions.

    Thread Starter ratamatcat

    (@ratamatcat)

    Hi David,

    I would like to add a second gallery to my attachment page that uses the same code fragment as you provided earlier:

    <?php
    $item_id = get_the_ID();
    $tags = get_the_terms( $item_id, 'attachment_tag' );
    $slug = '';
    foreach ( $tags as $tag ) {
    	if ( 0 === strpos( $tag->name, 'Gallery:' ) ) {
    		$slug = $tag->slug;
    		break;
    	}
    }
    
    if ( !empty( $slug ) ) {
    	echo do_shortcode( sprintf( '[mla_gallery attachment_tag="%1$s" ]', $slug ) );
    }
     ?>

    I have copied the fragment and pasted into a new location. This second gallery is to use the attachment tag term ‘Overlay: term’ (with term being different for each att. page gallery) therefore to the code I changed the following only:

    if ( 0 === strpos( $tag->name, 'Overlay:' ) ) {

    So far this second gallery is not showing up. Do I need to modify the shortcode or something else to differentiate it?

    thanks!

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your update and for listing the code fragment.

    You wrote “this second gallery is not showing up.” I assume that means you have both of the [mla_gallery] shortcodes on the same page. I don’t see any reason that they wouldn’t work as you intend.

    It would help to see all of the code for that page or at least all the code from get_the_terms() through the second do_shortcode(). Have you tried adding mla_debug=true to the second shortcode to see if it is simply returning an empty gallery?

    Any additional information you can provide will be of help; thanks.

    Thread Starter ratamatcat

    (@ratamatcat)

    Hello,

    Please see the link below. The first MLA shortcode starts on line 155 (this gallery is the one that’s displaying successfully). The second MLA shortcode starts on line 292

    https://drive.google.com/open?id=1vt080v5LMKy3bYOKznjLal4REbwy-DIT

    Also, the att. tag terms I used can be seen:

    https://drive.google.com/open?id=1QUcSWnqHz57UPJfaV9cXV0xRqROKRgAx

    i.e. the first and last ones in above screen capture. These are applied to the relevant media files

    I inserted mla_debug=true as below:

    [mla_gallery attachment_tag="%1$s" mla_debug=true]

    This made no visible change.

    Thanks,

    Plugin Author David Lingren

    (@dglingren)

    Thanks for the code and the terms image. As I wrote earlier, the code looks fine.

    If you added mla_debug=true to the shortcode and nothing was displayed it indicates that the shortcode did not execute; it will display quite a bit of (ugly) information even for an empty gallery. You can verify this by adding the parameter to your first, working, shortcode.

    That suggests that the $slug is empty. Perhaps adding some debug statements will provide more information, e.g., something like:

    // Use prefix word 'Overlay:' for each image in a set i.e. 'Overlay: caves sun campfires'
    $item_id = get_the_ID();
    $tags = get_the_terms( $item_id, 'attachment_tag' );
    echo sprintf( 'Tags for item %1$d = %2$s', $item_id, var_export( $tags, true ) );
    $slug = '';
    foreach ( $tags as $tag ) {
    	if ( 0 === strpos( $tag->name, 'Overlay:' ) ) {
    		$slug = $tag->slug;
    		break;
    	}
    }
    
    echo sprintf( 'Slug for item %1$d = %2$s', $item_id, $slug );
    if ( !empty( $slug ) ) {
    	echo do_shortcode( sprintf( '[mla_gallery attachment_tag="%1$s"]', $slug ) );
    }
    

    I did notice that you have 17 items assigned to “Portrayal: nebula stars dust” but only 7 assigned to “Overlay: nebula stars dust”. Is that a possible cause?

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘Gallery that uses the image Title’ is closed to new replies.