WordPress.org

Make WordPress Core

Opened 10 years ago

Last modified 4 months ago

#12799 new enhancement

Allow gallery shortcode to accept a maximum number of items

Reported by: dtorbert Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Gallery Keywords: dev-feedback has-patch
Focuses: Cc:
PR Number:

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 (5)

gallery shortcode replacement.txt (4.4 KB) - added by dtorbert 10 years ago.
Suggested replacement for gallery_shortcode()
12799.diff (2.4 KB) - added by jamescollins 7 years ago.
12799-2.diff (3.1 KB) - added by antpb 4 years ago.
12799-3.diff (3.0 KB) - added by antpb 4 years ago.
12799-4.diff (3.0 KB) - added by antpb 4 years ago.

Download all attachments as: .zip

Change History (25)

@dtorbert
10 years ago

Suggested replacement for gallery_shortcode()

#1 @nacin
9 years ago

  • Milestone changed from Awaiting Review to Future Release

#2 @BinaryMoon
7 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
7 years ago

#3 @jamescollins
7 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
7 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
4 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
4 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
4 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 4 years ago by antpb (previous) (diff)

@antpb
4 years ago

#8 @BinaryMoon
4 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
4 years ago

Thanks.

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

Last edited 4 years ago by Stagger Lee (previous) (diff)

#10 @antpb
4 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 4 years ago by antpb (previous) (diff)

@antpb
4 years ago

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


4 years ago

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


4 years ago

#13 follow-up: @michaelbeil
4 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
4 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
4 years ago

#15 @antpb
4 years ago

  • Type changed from feature request to enhancement

#16 in reply to: ↑ 12 @helen
4 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
4 years ago

  • Milestone changed from 4.4 to Future Release

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


4 years ago

#19 @helen
4 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.


4 months ago

Note: See TracTickets for help on using tickets.