Skip Navigation

[Resolved] How can I display the NAME of a taxonomy specified in the URL by its SLUG?

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

Problem:
I Query a view by a URL parameter where I check the SLUG of a taxonomy, but I want to display the NAME of the taxonomy queried in the URL parameter in my View.
Can I do that?

Solution:
You can use the ShortCode [wpv-search-term], which will listen to the URL parameter you pass to the View URL.
However this will output aways what you query by, so, if you query by the NAME of a Taxonomy, it outputs the name, if you query by a SLUG, it outputs the Slug.

Custom Code is needed, if you want to fetch the NAME of the taxonomy which is queried by SLUG.

A possible Custom Code Solution is to find here:
https://toolset.com/forums/topic/how-can-i-display-the-name-of-a-taxonomy-specified-in-the-url-by-its-slug/#post-393027

Relevant Documentation:
https://toolset.com/documentation/views-shortcodes/#wpv-search-term

This support ticket is created 6 years, 9 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 11 replies, has 4 voices.

Last updated by nicolaT-2 6 years, 9 months ago.

Assigned support staff: Beda.

Author
Posts
#391618
displaying-slug-name-problem.png

Please see the attached screenshot for what I hope will be a good illustration of the issue.

My challenge is that I can't figure out how to display the NAME of the taxonomy that I've filtered by specifying the SLUG in the URL. Here's the context:

When you're on a page like this one:
hidden link

There's a link in the sidebar to MORE which links to this page:
hidden link

Now look at my screenshot. What you'll see is that while I use wpvrelatedperson= to set the taxonomy to filter by, I'm using myname= as a work-around to display the name of the taxonomy.

It works great, but it opens me up to mischief (e.g. myname=Poopy+Mc.+Poopypants) or worse yet, HTML injection attacks from hackers.

SO: Given that my page has been passed in a taxonomy slug via the URL parameter wpvrelatedperson, how do I safely display the NAME of the taxonomy?

Thanks for your attention to this detail. It's critical to how our site functions.

- Canton

#391753

I don't understand your issue, I apologize.

The Views ShortCode and URL parameter, will take as values anything you stored as those values.

You can not change the URL for those.

Let's say, you have a Taxonomy and chose to query filter by the Taxonomy Slug passed in the ShortCode argument "mytaxarg"

Then you need to pass the Taxonomy Slug to your ShortCode Argument when inserting the View to a page.

This will not produce a URL but simply a filtered output.

Now if you pass the Argument as a URL argument, you will have to call the page where the View is, by passing the URL argument you want to display.

Assuming above exact same setup, but passed in the URL argument, you then receive the desired results by calling:

your-site.com/page-slug/?mytaxarg=your-term

Now if you want to use your taxonomy NAME instead of the SLUG in above URL parameter, you set this when you set up the Query Filter.
The Filter now looks like:

Select posts with taxonomy: your_tax name in one of those set by the URL parameter mytaxarg
eg. <em><u>hidden link</u></em>	

The URL can then look like this:

yoursite.com/page-slug/?mytaxarg=My%20Taxonomy%20Specifc%20Title

This is who the URL arguments work.

I apologie if I misunderstand you - can you please elaborate, in case I got it wrong?

Thank you

#391894
basic-settings.png

Hi,

Thanks for having a look, but I'm not sure I understand your reply. So, let's start over, and I'll try to state this more simply:

Please look at this page:

hidden link

On this view, how can I display a header which includes FULL NAME ("Véronique Salze-Lozac'h") of the person whose custom taxonomy slug is veronique-salze-lozach?

I have attached a screenshot which shows you the setup for this view.

The actual (simplified) code for the view is as follows, including the spot where I'd like to display the FULL NAME of the person who has been filtered. How do I show that?

[wpv-layout-start]
	[wpv-items-found]
<h2>*** HOW DO I INSERT FULL NAME OF WPVRELATEDPERSON HERE? ***: Blog posts, media coverage & more</h2><br />
	
	<!-- wpv-loop-start -->
<ul class="viewList">
		<wpv-loop>
          <li>[wpv-post-link]</li>
		</wpv-loop>
</ul>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
[wpv-layout-end]


#392069

I think you can achieve your Goal with the ShortCode [wpv-search-term]
https://toolset.com/documentation/views-shortcodes/#wpv-search-term

You can insert this above ShortCode with our GUI.

It will ask you to enter the Search term to listen to.

In your case this will be "wpvrelatedperson"

Then if you insert this ShortCode just after the [wpv-items-found], it will display the Search term which in your case is the SLUG if the related person.

This is what you are doing currently with your current setup.

The Search Term will always reflect what you pass in the URL.

If you need to display other things if the Search Term is a specific value you can use HTML Conditional.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

As example you can evaluate "If search term equals a given value, display this custom field"

I still do not really understand what you mean by the risk of attacks.

Of course the URL can be edited at any time by the Visitor, and if you construct your content on the URL parameters, it will also display it.
There is no way around this other then not building the content on other basis than the URL.

Maybe you want to pass this in a ShortCode attribute instead, then you can use the Attribute ShortCode as elaborated on the bottom of this DOC here:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/

Please let me know if you need further informations.

#392221
Screenshot 2016-04-25 10.59.01.png

I still do not feel like my question is being understood.

You said:

"Then if you insert this ShortCode just after the [wpv-items-found], it will display the Search term which in your case is the SLUG if the related person."

I already know how to display the SLUG of the related person.

My question is, how do I display the actual NAME of the related person (custom taxonomy) that my page is filtering.

So, for this specific page:

hidden link

How do I show the filtered wpvrelatedperson's full NAME?

- canton

#392441

I understood this, and elaborated how it's possible (or not)

wpv-search-term will always plot the search term

You search by SLUG and there fore the SLUG will be plotted.

wpv-search-term has no arguments to plot something that is not the actual Search term itself.

So to Display what you need, you also need to query by this.

In your case, you will change your query filter to filter by taxonomy NAME and then call the View with the correct URL syntax including the NAME of the taxonomy and this will be returned by wpv-search-term

Example:

Select posts with taxonomy: {your-tax} slug in one of those set by the URL parameter wpvcategory
eg. <em><u>hidden link</u></em>	

And called by this URL:

<a href="your site.com/page-with-views/?wpvcategory=all-capital-names-slug">LINK TEXT</a>

Will return the Posts where Taxonomy Slug is "all-capital-names-slug" and plot "all-capital-names-slug" where you inserted the [wpv-search-term]

If you now do this in your query:

Select posts with taxonomy: {your-tax} name in one of those set by the URL parameter wpvcategory
eg. <em><u>hidden link</u></em>	

And call it by this URL:

<a href="your site.com/page-with-views/?wpvcategory=All Capital Names Title">LINK TEXT</a>

It will plot "All Capital Names Title" where you inserted [wpv-search-term]

As you see, [wpv-search-term] will always return Search term
And you can choose to query either by Title or Slug, in a Taxonomy Query Filter by URL argument.

Thank you

#392531

Hello again, and thank you for your patience.

I believe I understand your recommendation:

1) Change my filter so that it expects the taxonomy NAME in the URL instead of the SLUG
2) Then I can just display the search term using [wpv-search-term param="wpvrelatedperson"]

This works great in some cases, for example when looking for posts related to Steven Rood:

hidden link

However, it totally fails for a more international name like Véronique Salze-Lozac’h. Both this URL:

hidden link

and this URL:

hidden link Salze-Lozac’h

fail to come up with any results (whereas there should be dozens.)

Matching taxonomies by NAME just isn't as reliable as matching by SLUG.

This being the case, do you have any other recommendations to solve my problem, which is generally:

When a view filters a taxonomy by URL (using SLUG, which appears to be our only reliable option), how can I retrieve the NAME of the taxonomy that's been filtered?

Perhaps I need a special function that retrieves the NAME of a taxonomy from the SLUG? If that's the case, can you provide me with the general "psuedocode" for this -- not the actual functioning PHP (which I'm I'm sure I can write) but the general idea as to how I would integrate my own function into this view?

Thanks again for your patience with me. I understand that this is a tricky question -- though I suspect this isn't highly unusual. Wanting to display the name of the thing you're filtering by must be a fairly common need.

#392878

As I said, [wpv-search-term] will listen to the URL parameter you pass to it.

The process to display the NAME is outlined in my previous post.
Same process is valid to display the SLUG.

What is printed by [wpv-search-term] is determined by what you search by.

If you want to search by SLUG it will output SLUG, if you search by NAME, it outputs NAME.

1. When a view filters a taxonomy by URL (using SLUG, which appears to be our only reliable option), how can I retrieve the NAME of the taxonomy that's been filtered?

You can not, with the ShortCode we provide for it,. It listen to the Search parameter which is what you set in the Query Options.

You can of course create your own custom ShortCode that:

1. Gets the URL search parameter
2. Fetches this SLUG in the Database (fetches the Taxonomy) and retrieves it's Name
3. Plots this (returns it) on your Website when inserted to the View.

Get the URL Param:
Here is some additional information how you could achieve this:
https://codex.wordpress.org/Function_Reference/get_query_var
hidden link
http://wordpress.stackexchange.com/questions/124542/how-to-pass-a-search-get-parameter-to-a-new-custom-search-page
http://stackoverflow.com/questions/8469767/get-url-query-string
http://stackoverflow.com/questions/11480763/how-to-get-parameters-from-this-url-string
http://stackoverflow.com/questions/5884807/get-url-parameter-in-php

Fetch the Taxonomy:
https://codex.wordpress.org/Function_Reference/get_term_by
https://wordpress.org/support/topic/display-current-taxonomy-name-on-taxonomyphp
hidden link
http://wordpress.stackexchange.com/questions/16394/how-to-get-a-taxonomy-term-name-by-the-slug

And to create a ShortCode:
https://codex.wordpress.org/Shortcode_API

Thank you

#393010

Okay, thanks for your reply.

As I understand it, the conclusion for this ticket is as follows:

If I filter a view by a taxonomy SLUG via the URL (which is the *only* way I can reliably filter) then it is IMPOSSIBLE for views to display the corresponding taxonomy NAME.

So the best solution is for me to write my own shortcode in PHP which will find corresponding NAME and output it, e.g.

[GET-RELATED-PERSON-NAME-FROM-SLUG slug="[wpv-search-term param="wpvrelatedperson"]]

I assume that there will be no problem nesting two shortcodes within one another like this in views.

Thank you for the recommendation. I'll do this.

#393027

In case it helps anyone else, here's how I solved the problem:

The view inserts this shortcode-within-a-shortcode:

<h2>Full Name: [GET-NAME-FROM-SLUG slug='[wpv-search-term param="wpvrelatedperson"]' taxonomy='related-person']</h2>

And this function (added to functions.php) provides us with the GET-NAME-FROM-SLUG functionality, for example:

[GET-NAME-FROM-SLUG slug="steven-rood" taxonomy="related-person"]

add_shortcode('GET-NAME-FROM-SLUG','do_get_name_from_slug');
function do_get_name_from_slug($atts) {
	extract(shortcode_atts(array("slug" => '', "taxonomy"=> ''), $atts));
	if (!$slug || !$taxonomy) return ('<p>Usage: GET-NAME-FROM-SLUG slug="target-slug" taxonomy="taxonomy-slug"</p>');
	// find taxonomy name using slug
	$nameObj = get_term_by('slug', $slug, $taxonomy);
	if ($nameObj) {
		return ($nameObj->name);	
	} else {
		return ('');
	}
}
#1142285

Thank you so much; I just had the same problem 😉

#1462383

Thanks a lot Canton!!! Your solution is very helpful!!!
Nicola