Skip Navigation

[Resolved] Displaying the name of the current category

This thread is resolved. Here is a description of the problem and solution.

Problem:
How can you display the name of the current term on a taxonomy archive page outside of the loop.

Solution:
It is currently not possible, though is an obvious omission, and the feature should be added soon.

In the meantime you can register a custom shortcode to output the term, which will output the name by default but when using the field argument can optionally output and of the name, slug, the description, count, or the taxonomy itself.

/*
 * Output taxonomy fields on a taxonomy archive
 *
 * attribute 'field' : name | slug | taxonomy | description | count
 */
add_shortcode( 'taxonomy', function( $atts = [] ){
 
    // default to taxonomy name
    $atts = shortcode_atts( [ 'field' => 'name' ], $atts );
  
    $output = "";
 
    global $wp_query;
 
    $tax = $wp_query->get_queried_object();
 
    $field = $atts['field'];
 
    $output = $tax->$field;
  
    return $output;
 
});
This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidR-12 6 years, 4 months ago.

Assisted by: Nigel.

Author
Posts
#1070375

I have a view set up for a particular category. However, when on the category term page I cat seem to output the particular term name. I have tried these:

  [wpv-post-taxonomy type="download_category" format="name"]
  [wpv-post-slug]
  [wpv-post-title]

The first outputs nothing, the second and third output "archive-for-module-categories" and "Archive for Module Categories" respectively which is the name of the view.

The URL I am on is hidden link and I want it to display the name of the category ( "body image").

Regards,

David

#1071278

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi David

Oddly enough it is not possible.

We have a request to update the taxonomy shortcodes (such as wpv-taxonomy-title) so that they work in the context of taxonomy archives and not just in their current scope of taxonomy Views.

In the meantime you can register a custom shortcode to output the taxonomy on a taxonomy archive:

/*
 * Output taxonomy fields on a taxonomy archive
 *
 * attribute 'field' : name | slug | taxonomy | description | count
 */
add_shortcode( 'taxonomy', function( $atts = [] ){

	// default to taxonomy name
	$atts = shortcode_atts( [ 'field' => 'name' ], $atts );
 
	$output = "";

    global $wp_query;

    $tax = $wp_query->get_queried_object();

    $field = $atts['field'];

    $output = $tax->$field;
 
    return $output;

});

By default it will output the taxonomy term name, but you can also use the field attribute to output the slug, the name of the taxonomy, the term description, or post count.

#1071552

Hi Nigel,

Thanks for the quick help. Yes I definitely put this issue in the "I'm sure this should be working " category. Anyway, that shortcode works perfectly.

Regards,

David