Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WPML echo url that switches to the correct url on translation WordPress

  • SOLVED

Hi

I've run into an issue with WMPL plugin.

I have various links hard coded into my theme.


<a href="<?php bloginfo('url'); ?>/riders" title="" class="button white-black pie ">
<?php _e('view the riders page','my theme'); ?>
</a>


If I switch to another language, and then click this button, I get a 404. For example if I was viewing Spanish, the URL would need to be... <?php bloginfo('url'); ?>/jinetes/?lang=es

But when I switch language, the link does not change.

I read this documentation...

[[LINK href="http://wpml.org/documentation/support/creating-multilingual-wordpress-themes/hard-coded-theme-links/"]]http://wpml.org/documentation/support/creating-multilingual-wordpress-themes/hard-coded-theme-links/[[/LINK]]

...and this bascially says you can only output full a tags, which is not what I'm after. I after just the URL.

I don't want to start messing with my css to accomodate these links by using <?php icl_link_to_element(10); ?> I need to just echo the URL string.

Thanks

Answers (1)

2012-01-27

Christianto answers:

Hi,

You can use icl_object_id to return id of the page, if you supply it with permalink you can use this function, please try:
function get_permalink_tp_wpml($permalink = '', $lang = 'es', $id = '', $post_type = ''){

if(!empty($permalink)){
$target_id = url_to_postid($permalink);
} else {
$target_id = $id;
}

if(empty($post_type)){
$post_type = get_post_type($target_id);
}

if($target_id){
$lang_id = icl_object_id( $target_id, $post_type, true, $lang);
return get_permalink($lang_id);
}
return $permalink;
}


and called it with url to be change as parameter for example get_bloginfo('url').'/riders'

echo get_permalink_tp_wpml( get_bloginfo('url').'/riders');


Or you can pass id and post type.

Btw, off topic, Have you tried my code on [[LINK href="http://wpquestions.com/question/show/id/3689"]]your question here[[/LINK]]?
could you try it, because this day is the last day before the prize goes to community pot, and tomorrow you cannot vote anymore. Hope you don't mind to try it..

Thanks


Josh Cranwell comments:

Hi Christianto,

Thanks for this I will have ago in 2 secs

Also on that question, tomorrow is the evening I will be testing your code. So I will be in touch for sure :-)


Josh Cranwell comments:

I got to go to bed, I will check this out tomorrow. THanks


Christianto comments:

Ok, have a nice dream, enjoy it.. ;-)


Josh Cranwell comments:

I managed to get it to work with this...

<?php $permalink = get_permalink( icl_object_id(20, 'page', false) ); echo $permalink ?>