Reputation: 460
This multilingual site generates url parameter at the end :
I want to precise the hreflang in the header, so I wrote :
<link rel="alternate" href="<?php echo get_permalink('') ;?>" hreflang="fr-fr" />
It works but then I get the url + the parameter, hence the hreflang is flase :
<link rel="alternate" href="https://www.example.fr/domaine/activite/industrie/?lang=fr" hreflang="fr-fr">
<link rel="alternate" href="https://www.example.fr/domaine/activite/industrie/?lang=fr" hreflang="en-fr">
Here is the result I want :
<link rel="alternate" href="https://www.example.fr/domaine/activite/industrie/?lang=fr" hreflang="fr-fr">
<link rel="alternate" href="https://www.example.fr/domaine/activite/industrie/?lang=en" hreflang="en-fr">
How can I retrieve separately the URL and the parameter ?
Upvotes: 2
Views: 1591
Reputation: 460
Tanks to Laken I could achieve what I wanted with a little change :
Implementation :
<link rel="alternate" href="<?php $wp_perma = get_permalink( '' );
$lang = $_GET['?lang'];
$wp_perma = str_replace( '' . $_GET['?lang'], '', $wp_perma );
echo $wp_perma ;?>" hreflang="x-default" />
As a result :
<link rel="alternate" href="https://www.example.fr/domaines/activite/?lang=en" hreflang="x-default">
So it's working fine for me.
Upvotes: 1
Reputation: 328
You're using get_permalink()
which is the first good step. It seems that there isn't anything built into that Core WordPress function to strip the GET parameters, but here's a little hack you can do:
First, figure out what the GET parameter is:
$lang = $_GET['lang']
Then we need to remove the GET parameter from the permalink... which is tricky. The simplest way is to just remove ?lang=$lang
but, if you have any permalinks which have query strings, that may cause some issues.
If the permalink that you need to echo doesn't have other query strings, then this is how you can do that:
$wp_perma = get_permalink( '' );
$lang = $_GET['lang'];
$wp_perma = str_replace( '?lang=' . $_GET['lang'], '', $wp_perma );
echo $wp_perma
Upvotes: 1