Skip Navigation

[Résolu] use javascript to link taxonomy to page

This support ticket is created Il y a 3 années. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Africa/Casablanca (GMT+01:00)

Marqué : 

This topic contains 18 réponses, has 2 voix.

Last updated by davidm-13 Il y a 3 années.

Assisted by: Jamal.

Auteur
Publications
#2009709

Hi
I'm using mctagmap plugin - https://wordpress.org/plugins/multi-column-tag-map/ - to list taxonomies. Clicking a taxonomy in the list takes me to the taxonomy archive page, I want to link to the custom post where the taxonomy appears - there's only one post per taxonomy.
I found this javascript to link to a different page

<script>
jQuery( document ).ready(function($) {
 // enable this script only on your specific page
 if( $("body").hasClass("page-id-2135") ) {
   var $tagLink = $("#mcTagMap").find("ul.links").find("a");   
   $tagLink.each(function() {
      var $t = $(this);
      $t.replaceWith( "<span class='keyword-tag'>"+ $t.text().trim() +"</span>" );
   });
 }
});
</script>

here - hidden link

Please could you show me the changes I need to make to link to the custom post that the taxonomy appears on.
Thanks

#2009755

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

This Javascript code will not actually change the links, it will remove the links and replace them with non-clickable strings(span tags).
I don't think that Javascript only can help here, because, at this stage, we cannot know what is the link of the post in the selected taxonomy. You will need a combination of PHP and Javascript codes. The PHP code needs to generate some kind of index that will hold the taxonomy and the link to the post on it. Then the Javascript code can change the links of the taxonomy terms with the post's link from the index generated by PHP. Does it make sense?

However, this may be a bit complicated, if you are not comfortable with PHP debugging consider hiring a developer. Check our partners' list here https://toolset.com/contractors/

#2009775

Hi Jamal, Thanks for your quick and detailed response.
A question - clicking on a taxonomy without adding any code links you to the taxonomy archive page, would it be possible to redirect the url to the required custom post - maybe attach the taxonomy to the post - there's one post for each taxonomy.

Thanks

#2009827

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Well, because each taxonomy term has only one post, you can build a redirection rule such as the following:

function redirect_my_custom_tax_archive_to_post() {
    if (is_tax('the-taxonomy-slug-here')) {
        global $post;
        wp_safe_redirect(get_permalink($post->ID));
        die;
    }
}
add_action('template_redirect','redirect_my_custom_tax_archive_to_post');

Adapt the second line to your custom taxonomy's slug.

But, if you want to change the generated URLs from WordPress, you will need a different custom code. If you want to change the generated URLs from the Multi-column plugin, you will need a different code.

#2009923

Removed duplicate repiy
don't see it so here it is again:
Thanks Jamal for the code, much appreciated.
Sorry, need help wth the javascript and implementation

If I use your code is it something that runs once or each time I click a taxonomy.

For example I have a taxonomy slug: english-name
with values : Cassie, Huisache, needle bush
All these values need to redirect to: site-name.org/?plant=acacia-farnesiana

I have around 300 plant - url's each with 1 - 8 taxonomies like the example above

So what would be the best way to implement this.

Thanks for your help

#2009969

can't login - don't see my last reply:
Thanks Jamal for the code, much appreciated.
Sorry, need help wth the javascript and implementation

If I use your code is it something that runs once or each time I click a taxonomy.

For example I have a taxonomy slug: english-name
with values : Cassie, Huisache, needle bush
All these values need to redirect to: site-name.org/?plant=acacia-farnesiana

I have around 300 plant - url's each with 1 - 8 taxonomies like the example above

So what would be the best way to implement this.

Thanks for your help

#2009973

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

You said earlier that the taxonomy has one post for each term, and you want to redirect the term page to that post page, right?

So, I am getting confused. I need more details:
- Do Cassie, Huisache, needle bush all need to redirect to site-name.org/?plant=acacia-farnesiana?
- Plant is the custom post type, right? What are the taxonomies?
- Let's take one taxonomy as an example, can you provide two terms, their URLs, and to what posts they should redirect?

The code that I suggested will redirect only the term archives from the taxonomy with slug "the-taxonomy-slug-here". Does it make sense?

#2010003

Hi, Jamal,
I can't login to toolset support, so only see your reply in email.

Sorry I wasn't clear.
Yes plant is the custom post type.
Every plant post can can have 1 - 8 english-name taxonomies but each taxonomy only appears on a single plant post - so each plant can have several english-name taxonomies.

example - 2 english-name taxonomies
Term: Bearded Darnel url: /?s=Bearded%20Darnel redirect to: /?plant=Lolium-Termulentum
Term: Caper Bush url: /?s=Caper%20Bush redirect to: /?plant=Caparis-Zoharyi

#2010005

- Do Cassie, Huisache, needle bush all need to redirect to site-name.org/?plant=acacia-farnesiana?

Yes

#2010343

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

If you can't connect to the forum, you need to clear the browser data or try in a different browser, you should be able to login and read all the replies.

Can you tell what are the taxonomies of these terms? What is the slug of each taxonomy?
- Bearded Darnel
- Caper Bush

Can you explain what do you mean by this /?s=Bearded%20Darnel redirect to: /?plant=Lolium-Termulentum ?
This seems to be the URL of a WordPress search, Are you looking to redirect that too?

#2010497
tax2plant.JPG
mctagmap.JPG
tax-english-name.JPG
click-darnel-tax.JPG

Hi Jamal,
Hope these screen shots make it clearer - can be I didn't set up the taxonomy correctly so clicking it is bringing up the wordpress search page and not the archive page.

tax-english-name.jpg: taxonomy page
mctagmap,jpg: list from matagmap
tax2plant.jpg: page I want to redirect to.
click-darnel-tax.jpg; page I recieve clicking on Darnel Rye-grass on list

Thanks

#2010701

Can be mctagmap plugin - https://wordpress.org/plugins/multi-column-tag-map/ - returns the wordpress search when clicking items in the list

#2010853

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Thank you for the screenshots. Below what I understand, please correct me if I am wrong:
- Lolium temulentum is a post from the custom post type Plant.
- Lolium temulentum has 3 terms(Bearded Darnel, Darnel, Darnel Rye-grass, Poison Rye-grass) for the taxonomy "English name(s)".
- Each term of the taxonomy "English name(s)" has only one post. (Attached image tax-english-name.JPG)
- You don't want to use term archive pages for this taxonomy, and you want to use the post linked to it instead.

Now, I need to know, where do you use the "multi-column-tag-map" plugin? I assume in the "Species list" page hidden link check this screenshot hidden link
If that's the case, the plugin is generating search URLs instead of term page archives. Can you tell me how do you generate this list? what shortcode are you using?

#2011031
taxonomy.JPG

Hi Jamal,
You understand correctly.
The the "multi-column-tag-map" plugin is used on Speices List
The shortcode I'm using to generate the list is:

[mctagmap columns="3" post_type="plant" taxonomy="english-name" width="320" show_navigation="yes" 
 manual="a,b,c"]

Using the above, clicking on a taxonomy term gives the attached screen shot - the previous screenshot included search="yes" as one of the parameters in the shortcode .

#2011079

Jamal
Supporter

Languages: Anglais (English ) Français (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Awesome. Now you would like the archive page to redirect to the assigned post page. For example:
- Archive page hidden link
- Should redirect to post hidden link
Right? If that's the case, you can adapt the custom code to:

function redirect_my_custom_tax_archive_to_post() {
    if (is_tax('english-name')) {
        global $post;
        wp_safe_redirect(get_permalink($post->ID));
        die;
    }
}
add_action('template_redirect','redirect_my_custom_tax_archive_to_post');
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.