Skip Navigation

[Resolved] Only call one taxonomy term for use in a view when a post has several

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

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 3 replies, has 2 voices.

Last updated by liatG 6 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#1130204
Chapter view for sidebar.png
Toe-Up Sock Pattern.png

I'm using a custom taxonomy to insert a URL into a view that is used as a navigation menu, with an anchor, like this:

<a href="[wpv-post-taxonomy type="my-classes" format="url"]#[wpv-post-slug]">Post title</a>

This is how all my navigation works across many different classes. Almost all the posts have just one taxonomy term (they belong to only one class). I need help in the case that the post has more than one taxonomy term, that is, I use it for more than one class.

This is only the case for only about 75 out of the around 500 posts of this type, so if I need to do something manually to make this work, that's ok. But I can't figure out how to just call the one taxonomy term.

Here is an example - if the post has taxonomy terms "knitting-superstar," "magic-loop-socks," and "toe-up-socks," the URL returned in the navigation looks like this:

<a href="/knitting-superstar/post-slug, /magic-loop-socks/post-slug, toe-up-socks/post-slug.">Post title</a>.

Clicking that link takes me to the first URL in that list, which is not the right one.

For the navigation, I need to specify that the chapter belongs to a certain term only. When I'm on the page for toe-up-socks archive, that chapter belongs to that term.

You can see in the screenshot that when I hover over the link for "Toe-Up Sock Pattern," the URL preview at the bottom shows the comma-separated list of URLs. I need it to go to just one URL - the right one.

Here's that page: hidden link

Can you help me think of a way to only return one term's URL just for certain classes?

Here is the view that displays the sidebar menu items - the code at issue is highlighted in the screenshot.
hidden link

I'm happy to create another taxonomy or custom field to label these special posts in some way, but I don't know how to limit or filter the results of this code [wpv-post-taxonomy type="my-classes" format="url"].

Thank you,
Liat

#1130649

Hi Liat,

Thank you for contacting us and I'll be happy to assist.

The [wpv-post-taxonomy] shortcode doesn't include any built-in parameter to limit the number of the attached terms.
( ref: https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153472 )

To achieve this, you can create a custom shortcode and use it inside the view.
( ref: https://codex.wordpress.org/Shortcode_API )

For example, you can add the following code at bottom of your active theme's "functions.php" file, to register a new shortcode [show-single-classes-link]:


add_shortcode('show-single-classes-link', 'show_single_classes_link_func');
function show_single_classes_link_func() {

	$my_classes_data = do_shortcode('[wpv-post-taxonomy type="my-classes" format="url" separator="***"]');

	$my_classes_arr = explode("***",$my_classes_data);

	return $my_classes_arr[0];
}

It will show only the first term's URL when used inside the view.


<a href="[show-single-classes-link]#[wpv-post-slug]">Post title</a>

Similarly, you can adjust the PHP code inside the shortcode to filter or shortlist the terms as needed.

For a more personalized and detailed assistance around custom code, you can also consider consulting, one of our certified consultants:
https://toolset.com/contractors/

I hope this helps.

regards,
Waqar

#1131263
Show single classes link not working.png

Hi Waqar, thank you so much for writing this custom code for me. I need a little further help. Would you mind passing this question onto Christian Cox for me? He has helped me with many tickets and I'd like to request his help in resolving this.

Christian,
This didn't work for me. I added this code as a Custom code snippet in my Toolset settings. I wasn't sure whether to also register this as a third-party shortcode, but, trying it both ways, it didn't work. I added this to my view inside the link (see screenshot - you can see how the link shows up in the URL preview in the left-hand corner) and also outside the link just to view the results. It just prints the shortcode, it doesn't return any URL.

Do you think you can help me write something that will return the single URL?

ALSO - I need to be able to specify WHICH URL. The first one isn't necessarily the one I need to call. We can maybe use the [get-current-slug] shortcode to specify which of the taxonomy terms to link to... or some other way to identify which URL to use. Speaking in plain English, it would be the class part that is part of the class the user is currently looking at.

I'm not sure what the smartest way to do this is. I just know I need to do using logic instead of manually separating out these classes, because today I tried duplicating all the chapters to create totally separate classes, and it's not feasible. I will end up with 3x and 4x duplicate content because these chapters are re-used in several classes, and it results in several hundred added chapters.

Thank you so much for your help. This is my last step in finishing my membership site and launching it to my clients.

Thank you,
Liat

#1132073

Thank you!