Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#27238 closed enhancement (fixed)

Add parameters to the_taxonomies() and get_the_taxonomies() to deselect linking

Reported by: davidjlaietta's profile davidjlaietta Owned by: boonebgorges's profile boonebgorges
Milestone: 4.1 Priority: normal
Severity: normal Version: 3.8
Component: Taxonomy Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Currently get_the_taxonomies() automatically wrap all taxonomies that they list in anchor tags:

$links[] = "<a href='" . esc_attr( get_term_link($term) ) . "'>$term->name</a>";

We would like to have the option to remove these, to display an unlinked list of taxonomies that are applied to a post:

$list[] = $term->name;

The patch that I've attached includes a 'links' parameter to both the_taxonomies() and get_the_taxonomies(), set to true to not affect existing usage of these functions. If the parameter is set to false, only the taxonomies will be returned, and not their links.

Attachments (6)

27238.diff (13.9 KB) - added by davidjlaietta 11 years ago.
27238.2.diff (1.8 KB) - added by hereswhatidid 11 years ago.
Added term_template parameter to support modifying the individual term list items
27238.3.diff (1.8 KB) - added by hereswhatidid 11 years ago.
Made the term_template default value more in line with what the documentation would support.
27238.4.diff (1.9 KB) - added by hereswhatidid 10 years ago.
Fixed replace string name to be more consistent.
27238.5.patch (5.6 KB) - added by dlh 10 years ago.
27238.6.patch (5.0 KB) - added by dlh 10 years ago.

Download all attachments as: .zip

Change History (20)

@davidjlaietta
11 years ago

#1 @hereswhatidid
11 years ago

This can already be accomplished with the 'template' argument. You would just call the function like so:

	$args = array(
		'template' => '%s'
	);
	$taxonomy_list =  get_the_taxonomies( null, $args );

then $taxonomy_list would contain an array of just the term labels.

Last edited 11 years ago by hereswhatidid (previous) (diff)

#2 @SergeyBiryukov
11 years ago

  • Description modified (diff)
  • Version changed from trunk to 3.8

Replying to hereswhatidid:

then $taxonomy_list would contain an array of just the term names.

That would print taxonomy names, but not term names. You can, however, use strip_tags() to get rid of links:

$taxonomies = array_map( 'strip_tags', get_the_taxonomies() );
echo implode( ' ', $taxonomies );

Looks like 27238.diff is made against 3.8 rather than current trunk, so it cannot be applied properly.

Last edited 11 years ago by SergeyBiryukov (previous) (diff)

@hereswhatidid
11 years ago

Added term_template parameter to support modifying the individual term list items

@hereswhatidid
11 years ago

Made the term_template default value more in line with what the documentation would support.

#3 follow-up: @hereswhatidid
11 years ago

The 'term_template' argument would enable not only limiting what is displayed but adding more custom elements to the output. Here's an example with the term name only wrapped in a span:

	$args = array(
		'term_template' => '<span>%2$s</span>'
	);
	get_the_taxonomies( null, $args );

and one that would open the category links in a new window:

	$args = array(
		'term_template' => '<a href="%1$s" target="_blank">%2$s</a>'
	);
	get_the_taxonomies( null, $args );

#4 in reply to: ↑ 3 @davidjlaietta
11 years ago

That would work for me, and makes more sense to allow more control over the template than just adding or removing links.

Replying to hereswhatidid:

The 'term_template' argument would enable not only limiting what is displayed but adding more custom elements to the output. Here's an example with the term name only wrapped in a span:

	$args = array(
		'term_template' => '<span>%2$s</span>'
	);
	get_the_taxonomies( null, $args );

and one that would open the category links in a new window:

	$args = array(
		'term_template' => '<a href="%1$s" target="_blank">%2$s</a>'
	);
	get_the_taxonomies( null, $args );

#5 follow-up: @boonebgorges
10 years ago

  • Keywords needs-unit-tests added

Agreed that 'term_template' is a better, more general approach.

hereswhatidid - Question about the default value. If you're going to use %2$s for argument swapping, why not use %1$s for the first value as well?

Would like to see unit tests demonstrating the new functionality, and especially showing that the default output remains the same as it currently is.

#6 in reply to: ↑ 5 @hereswhatidid
10 years ago

Replying to boonebgorges:

Agreed that 'term_template' is a better, more general approach.

hereswhatidid - Question about the default value. If you're going to use %2$s for argument swapping, why not use %1$s for the first value as well?

Would like to see unit tests demonstrating the new functionality, and especially showing that the default output remains the same as it currently is.

The first value was just a copy/paste mistake. %1$s is what should be there.

@hereswhatidid
10 years ago

Fixed replace string name to be more consistent.

@dlh
10 years ago

#7 @dlh
10 years ago

27238.5 adds tests to hereswhatidid's patch. It also restores a dropped __() around $template and refreshes the inline docs to go along with the new parameter.

boonebgorges: The default output has a test in test_the_taxonomies(). Did you want additional ones?

#8 @DrewAPicture
10 years ago

So in looking at 27238.5.patch, it occurs to me that the default arguments are actually defined in the wrong function. They should be defined in get_the_taxonomies(). Let's handle moving that code to a separate patch after this is taken care of.

For now, let's move the more in-depth argument descriptions for $template and $term_template into the main hash notation that was added to the_taxonomies() and leave the $args parameter description on get_the_taxonomies() as-is for the time being.

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

@dlh
10 years ago

#9 follow-up: @dlh
10 years ago

27238.6.patch updates the argument descriptions per DrewAPicture's comment.

#10 in reply to: ↑ 9 @boonebgorges
10 years ago

  • Milestone changed from Awaiting Review to 4.1

#11 @MikeHansenMe
10 years ago

  • Keywords needs-unit-tests removed

27238.6.patch has unit tests

#12 @boonebgorges
10 years ago

In 30209:

Introduce term_template param to get_the_taxonomies().

This parameter allows theme and plugin authors to specify the formatting they
would like on the term links as they are parsed into the taxonomy list.

Props hereswhatidid, dlh, davidjlaietta.
See #27238.

#13 @boonebgorges
10 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 30210:

Improve docblocks for get_the_taxonomies() and the_taxonomies().

  • In the_taxonomies(), only document those arguments that are specific to the function. Arguments shared with get_the_taxonomies() should be documented in the later function's docblock.
  • Add $args hash documentation to get_the_taxonomies().
  • Remove the redundant default argument definition in the_taxonomies().

Fixes #27238.

#14 @dd32
10 years ago

In 30365:

Remove a stray translator comment for a non-translatable string added in [30209] See #27238

Note: See TracTickets for help on using tickets.