最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

conditional tags - A simple method for disabling WPML language switcher when meeting specific circumstances

programmeradmin5浏览0评论
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 4 years ago.

Improve this question

this is not a question but -hopefully- an answer. I realized that more than a couple of people on the WPML forum asked for a solution to this matter, so I thought it could be useful to share my workaround here as I did there.

The Issue

Let's say that for whatever reason you need to disable the WPML language switcher just for a specific page, or even for all posts, or all single pages in a category... literally for any reason.

For example I have a menu M1 in position m-1 with Home | about us | contact us, and with the switcher SW1 assigned to it via the WPML > languages page; let's say that I don't want SW1 to appear when I'm in the about us page.

The workaround

I just register another menu M2 -which is the exact copy of M1- and assign it to position m-2.

At this point I already have total control in my template files upon where and when SW1 will appear, using WordPress conditional tags or whatever other PHP condition.

For example in page.php I can do:

if ( is_page( 'about-us' ) ) {
    wp_nav_menu( array( 'theme_location' => 'm-2', (...etc) ) );
} else {
    wp_nav_menu( array( 'theme_location' => 'm-1', (...etc) ) );
}

Now SW1 will appear only when we are not in the about us page since it's assigned to m-1 and about us picks m-2.

Trivia

I had to found this workaround because WPML had problems with CPT date archives and I needed a fast idea. More specifically, if I had, say, "mysite/it/mia-pagina/2020/" I couldn't find a way to go to "mysite/en/my-page/2020/" via the default switcher (N.B. I had already enabled the date archives for CPTs thanks to a third party plugin that just adds the rewrite rules that in WordPress are missing for this scenario).

Funny enough, I could go to the desired page if I just manually wrote its url in the browser. It's just that the WPML switcher's link failed in this specific case.

So without the time to wait for a core update or for a deep and slow programming, I needed to disable the default WPML language switcher just for CPT date archives and substitute it with a custom one.

Hope it helps!

发布评论

评论列表(0)

  1. 暂无评论