• Hey there ,
    I am doing some SEO stuff and want to implement the Hreflang Tag.
    I am almost there ,however I have a few problems.
    1.How can I obtain the site url for different languages?
    -Basically a function that will get the url for different domains.Rignt now its based on language , for example in english it is example.com/en , and for default language it is example.com.
    2.Get list of all active languages , right now I have hardcoded my languages
    $languages = array('en_GB' , 'bg_BG');
    I want to do it dynamic for the code to be cleaner.
    3.The last one.How to obtain the different category url for different language.
    I am using this code for the ‘post’ and ‘page’ urls , but it doesnt seem to work for the category ,since the ID is different for different language.
    get_permalink( icl_object_id( get_the_ID() , 'post', true , $code) )

    • This topic was modified 8 years, 2 months ago by nikksan.
    • This topic was modified 8 years, 2 months ago by nikksan.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You could set up a rewrite rule and tag so the language becomes a query var, but without a base string that is static it’s difficult to write a regexp that’ll work unless the language term exists in all URLs. So it’s best if you always have something like example.com/lang/en where “lang” exists anytime a language term follows, the same way we see example.com/tag/foo for tag requests. Without that, the first term must always exist no matter what, even for the default language.

    You can also simply parse the term out of the request string in $_SERVER[‘REQUEST_URI’]), but you have a similar situation, either a base string is needed or the term must be there for all requests. There has to be some reliable way for code to know how to extract the language term out of everything else that might occur in an URL.

    The languages available are determined by the *.mo files in the languages folder. You can get this list (with “.mo” stripped out, leaving just lang_REGION) with get_available_languages().

    If the different category terms relate to the corresponding post ID of the respective language, you’d need to query for the posts ( using get_posts() or similar ) that are translations of the current post, then get the category terms of each translation, with which you can use get_category_link() to get the actual URL. This is assuming the only reference to language is associated with the post. If category language were separately managed through term meta, then it may be feasible to query for terms instead of posts.

Viewing 1 replies (of 1 total)
  • The topic ‘Get URL for different languages’ is closed to new replies.