Skip Navigation

[Resolved] Taxonomy list with conditional links in Toolset Blocks content template

This support ticket is created 2 years, 10 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 5 replies, has 2 voices.

Last updated by Annie 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2081871

I have a content template built in Toolset Blocks, which currently outputs a list of taxonomy selections for that post. I want to conditionally link some of those taxonomies, but not to the taxonomy pages. Basically if the taxonomy is this one specific entry, it should be linked to a certain page, but if it's anything else it should be linked to this one other page. How can I set that up?

#2082481

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

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

To confirm, you'd like to show the taxonomy terms attached to the current post, so that if it is a specific term (for example "Tag 2"), it should link to a special page ( for example: {yourwebsite.com}/special-page ). And if it is any other term, it should link to a common or generic page ( for example: {yourwebsite.com}/normal-page ).

If my understanding is correct, you'll need to use a custom shortcode for this:


function process_tax_links_func( $atts, $content = null ) {
	$a = shortcode_atts( array(
		'sep' => ', ',
		'term' => '',
		'term_link' => '',
		'generic_link' => '',
	), $atts );

	$content = apply_filters('the_content',$content);

	if(!empty($content)) {

		$final_content = array();

		$content_arr = explode($a['sep'], $content);

		for ($i=0; $i < sizeof($content_arr) ; $i++) { 
			if($content_arr[$i] == $a['term']) {
				$final_content[] = '<a href="'.$a['term_link'].'">'.$content_arr[$i].'</a>';
			}
			else
			{
				$final_content[] = '<a href="'.$a['generic_link'].'">'.$content_arr[$i].'</a>';
			}
		}
		return implode($a['sep'], $final_content);
	}
}

add_shortcode( 'process_tax_links', 'process_tax_links_func' );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

After that, you'll be able to use this new shortcode in your content template like this, inside a "Fields and Text" block:


[process_tax_links sep=", " term="Tag 2" term_link="{yourwebsite.com}/special-page" generic_link="{yourwebsite.com}/normal-page"][wpv-post-taxonomy type="post_tag" format="name"][/process_tax_links]

Please note how the original taxonomy shortcode ( [wpv-post-taxonomy type="post_tag" format="name"] ) is enclosed in the new shortcode, which will give the comma-separated list of current post's term names from the "post_tag" taxonomy, which you can change based on your target taxonomy.
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-taxonomy )

And then the "sep" attribute tells which separator is being used between the term names, "term" tells about the target special term name to look out for, and the "term_link" and "generic_link" attributes, represent the special term's and the generic term's links, respectively.

I hope this helps and please let me know if any point is not clear.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2084013

I tried that code, but for some reason even for the item that's supposed to have the custom link, it still just uses the generic one. I've copy/pasted the term name and checked for any blank spaces that might cause it to not match, but no matter what it never uses the special link. Here's the shortcode:

[process_tax_links sep=", " term="Search Engine Optimization" term_link="/digital-marketing-local-seo/" generic_link="/web-design-development/"][wpv-post-taxonomy type="project-type" format="name"][/process_tax_links]

And this is what it outputs:

<a href="/web-design-development/">Search Engine Optimization</a>, <a href="/web-design-development/">Website Design</a>

Both of those are copy/pasted, you can see the term in the shortcode matches the first item in the list, but it's not being matched with the special link. Any idea what's wrong?

#2088461

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and it is strange that it is not working on your website.

Can you please share temporary admin login details, along with the link to the page where this shortcode can be seen?

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

#2089527

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the admin access.

During testing, I was able to narrow down the conflict, due to using the shortcode into the "Fields and Text" block.

In the custom shortcode snippet that I shared with you earlier, at line# 10, you'll see this code:


$content = apply_filters('the_content',$content);

Please replace it with:


$content = do_shortcode($content);

This should do the trick.

#2089639

Perfect, thanks.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.