Make WordPress Core

Opened 15 years ago

Last modified 19 months ago

#12799 new enhancement

Allow gallery shortcode to accept a maximum number of items

Reported by: dtorbert's profile dtorbert Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 2.9
Component: Gallery Keywords: has-patch changes-requested close good-first-bug
Focuses: Cc:

Description

A "would be nice" feature of the gallery would be to allow for a maximum number of items to be displayed.

The main use of this feature would be to allow a page to show a "preview" of some of the images contained within one or more subpages, eg:

  • Some event

[gallery link="file" columns="4" orderby="rand" maximum="4" id="164"]

  • Some other event

[gallery link="file" columns="4" orderby="rand" maximum="4" id="200"]

Attachments (6)

gallery shortcode replacement.txt (4.4 KB) - added by dtorbert 15 years ago.
Suggested replacement for gallery_shortcode()
12799.diff (2.4 KB) - added by jamescollins 13 years ago.
12799-2.diff (3.1 KB) - added by antpb 10 years ago.
12799-3.diff (3.0 KB) - added by antpb 10 years ago.
12799-4.diff (3.0 KB) - added by antpb 10 years ago.
12799.2.diff (1.9 KB) - added by viralsampat 2 years ago.
I have checked it from my end and added my patch

Download all attachments as: .zip

Change History (28)

@dtorbert
15 years ago

Suggested replacement for gallery_shortcode()

#1 @nacin
15 years ago

  • Milestone changed from Awaiting Review to Future Release

#2 @BinaryMoon
13 years ago

Hi - I have a use case for this - specifically for gallery custom post types where I want to display a subset of the gallery on the archive/ homepage. As such - is there an update for when this might happen as I think it would be really useful.

@jamescollins
13 years ago

#3 @jamescollins
13 years ago

  • Keywords has-patch added

12799.diff adds an optional numberimages="" parameter to the [gallery] shortcode.

This parameter is named similarly to the numberposts attribute in get_posts().

If the numberimages parameter is set to a number greater than zero, then it controls the maximum number of images displayed by the [gallery] shortcode.

Examples:

[gallery link="file" columns="4" orderby="rand" numberimages="4" id="256"]
[gallery link="file" columns="4" orderby="rand" numberimages="4" include="263,264,261,260,259"]

In both cases, only 4 images (at most) will be displayed.

The code on line 836-839 is only necessary because in get_posts(), when specifying include="", the numberposts parameter is ignored (it's set to match the number if items in include="").

#4 follow-up: @BinaryMoon
13 years ago

that looks perfect for my needs - does this diff mean that it will be added to core, or is it just a proposal for inclusion (I'm new to Trac - don't know how it works)?

#5 in reply to: ↑ 4 @chriscct7
10 years ago

  • Keywords dev-feedback needs-refresh added; gallery maximum has-patch removed
  • Version changed from 2.9.2 to 2.9

Replying to BinaryMoon:

that looks perfect for my needs - does this diff mean that it will be added to core, or is it just a proposal for inclusion (I'm new to Trac - don't know how it works)?

It means it's something we can merge into core, but not marked for inclusion (that's the milestone for the ticket)

Other devs: Do we want to support a max param, or do we want to relegate that to a plugin to support?

#6 @antpb
10 years ago

  • Keywords needs-docs added

I like the idea of having a maximum number of images to display. I could see a use case of this if someone is wanting to randomly cycle through a preset list of images.

I like this...I'll work on updating the patch. I think this will also need docs on the new option.

#7 @antpb
10 years ago

  • Keywords has-patch added; needs-refresh needs-docs removed
  • Milestone changed from Future Release to 4.4

Woot! Below is the new patch. Tested on the nightly and it seems to be working like a charm now! After using it, I really like the addition. I think it could be useful. It's kinda cool refreshing the page and getting different random images.

I went with maxdisp as the shortcode attribute for the sake of keeping it short and sweet. Also added docs to the shortcode listing.

Usage looks like the below shortcode:

[gallery include="8,7,6,5,4" maxdisp="2" orderby="rand"]

Would still like a lead dev to thumbs up this so leaving dev feedback.

Patch below!

Last edited 10 years ago by antpb (previous) (diff)

@antpb
10 years ago

#8 @BinaryMoon
10 years ago

Nice - thanks :)

Not sure about maxdisp as the attribute name though. I'd imagine something like 'count' would be clearer?

To me maxdisp sounds very programmery (like a variable name) and I'm not sure the average user would understand it's use.

#9 @Stagger Lee
10 years ago

Thanks.

It is something I needed on several websites. If used in widget and Users dont have access to widget managament better to use "post ID" than "include".

Version 0, edited 10 years ago by Stagger Lee (next)

#10 @antpb
10 years ago

Completely agree. It's the robot in me that decided on maxdisp. I like count much better. Also, this works just the same with ids= as well.

[gallery ids="8,7,6,5,4" count="2" orderby="rand"]

Updated patch below!

Last edited 10 years ago by antpb (previous) (diff)

@antpb
10 years ago

This ticket was mentioned in Slack in #design by antpb. View the logs.


10 years ago

This ticket was mentioned in Slack in #design by antpb. View the logs.


10 years ago

#13 follow-up: @michaelbeil
10 years ago

This looks great @dtorbert!

@antpb your most recent diff has a few spacing issues in the first phpdoc block (L1397) and then the array (L1448).

Also, I noticed a typo on L1476:
Necessary because in get_posts(), when specifying include="", the numberposts parameter is ignored (it's set to match the number if items in include="")`

I believe you meant number of items.

#14 in reply to: ↑ 13 @antpb
10 years ago

Replying to michaelbeil: Thanks! Good catch. I actually copied the line from a previous diff.

I went ahead and fixed the spacing issue as well. Patch to follow...

This looks great @dtorbert!

@antpb your most recent diff has a few spacing issues in the first phpdoc block (L1397) and then the array (L1448).

Also, I noticed a typo on L1476:
Necessary because in get_posts(), when specifying include="", the numberposts parameter is ignored (it's set to match the number if items in include="")`

I believe you meant number of items.

@antpb
10 years ago

#15 @antpb
10 years ago

  • Type changed from feature request to enhancement

#16 in reply to: ↑ 12 @helen
10 years ago

Replying to slackbot:

This ticket was mentioned in Slack in #design by antpb. View the logs.

Having a UI for this by default does not make sense, though an unexposed shortcode attribute seems reasonable. See the discussion for more details.

#17 @wonderboymusic
10 years ago

  • Milestone changed from 4.4 to Future Release

This ticket was mentioned in Slack in #core by helen. View the logs.


10 years ago

#19 follow-up: @helen
10 years ago

I'm not sure count is the right name for the attribute either, even if that's what it's called in the code.

This ticket was mentioned in Slack in #core-editor by talldanwp. View the logs.


6 years ago

#21 in reply to: ↑ 19 @desrosj
3 years ago

  • Keywords needs-refresh changes-requested close good-first-bug added; dev-feedback removed
  • Milestone set to Future Release

With the introduction of the block editor and full site editing, shortcodes (though still supported) are no longer the recommended way to display a gallery.

With that in mind, I'm going to add a close suggestion as a maybelater. I won't close this out right now because I think it's a fair attribute to introduce, even in block editor times. If someone is interested in taking the time to create a new patch, this can be looked at. If there's no new activity in a few months, I'll return and close it out.

Because it has not received any attention in over 7 years, 12799-4.diff certainly will need a refresh.

Replying to helen:

I'm not sure count is the right name for the attribute either, even if that's what it's called in the code.

I also don't like count as the attribute name. limit feels like a more natural name to give this one.

@viralsampat
2 years ago

I have checked it from my end and added my patch

This ticket was mentioned in PR #5530 on WordPress/wordpress-develop by Lymnik01.


19 months ago
#22

  • Keywords needs-refresh removed

I renamed the shortcode parameter that limits the number of images shown by the schortcode to 'limit'.

Limiting the number of images should be possible for shortcodes using the include-parameter, exclude-parameter and for those that use neither. For that I combined some elements of the previous patch( 12799-2.diff ) with some elements of the one before that( 12799-4.diff ).

Trac ticket: https://core.trac.wordpress.org/ticket/12799

Note: See TracTickets for help on using tickets.