1

I've got always the same problem. If I program a multisite for multilanguage purposes, I cannot find a way to not change the page links in every language. I mean, suppose I've got a page named 'Activity' in the english version. Then got the Italian version, and the same page is called 'Attività'. If I've got a manually-setted link outside the menus, say:

get_page_by_title('Activity')

Then I've got to translate it in the IT theme:

get_page_by_title('Actività')

And so on for all the other languages.

So, is there a way to avoid having to check for all links in every language?

Thank you

5 Answers 5

1

For people, as me, that comes to this thread, looking for a solution, pll_get_post() is your function. I think this is the easiest solution for this problem.

    $the_translated_page = get_page_by_title( 'Custom page' );
    $the_translated_page = pll_get_post( $the_translated_page->ID );
    $the_translated_page_URL = get_permalink( $the_translated_page );

I'm pretty sure that this function (pll_get_post) was recently included in the plugin, or at least, it didn't exists in 2013...

Hope it helps!

2
  • Yes this, together with my own accepted answer, it's the fastest way, assuming the usage of the polylang plugin. If the use case involves specific templates, than mine is the way. If the need is to somewhat reference a specific page that doesn't have a specific template, than your way is the way :) So thank you for adding. Commented Feb 26, 2016 at 8:51
  • Completely agree! cheers!
    – Capiedge
    Commented Feb 26, 2016 at 12:05
0

Translate the title before you query for it:

get_page_by_title( __( 'Activity', 'your_text_domain' ) );

Or store the relationship between posts in a separate table like Multilingual Press does it. Then look up that table for the associated post.

2
  • both the solutions you are suggesting imply some manually added translation, if I'm not wrong. What I'm searching for is a way to get the link without making any translation, since we are considering really the same page in diffent languages. What would be good, is a single piece of information attached to the page and unique for all its 'translated' counterparts. For example, I hoped I could use the slug, but I cannot do it since in some cases it's binded with the permalink, so in that case I coundn't have for example, the same slug for all languages of that page but different permalinks. Commented Apr 16, 2013 at 11:00
  • The second option requires just a click to mark two posts as related.
    – fuxia
    Commented Apr 16, 2013 at 11:06
0

My solution to my problem: custom fields. Basically, you can create a custom field (with the default admin tool) and call it say, "class", or whatever you want. You can then assign it a value that will be the same for all languages of the same page. What is good is that in fact, if you use the default xml export>import tool to copy the page structure in all the languages/sites, the custom field will be retained, so you have to write it only once per page.

That said, I also made 2 raw and small functions and I want to share them.

I use this one instead of is_page(), to set different css and js files:

function has_page_class($page_class){
global $wpdb;
$id = get_queried_object_id();

$check = $wpdb->get_var($wpdb->prepare(
"SELECT COUNT(*)  
FROM $wpdb->postmeta m 
WHERE m.post_id = %d and m.meta_key = 'class' and m.meta_value = %s",
$id, $page_class));

if($check) return true;

return false;
}

And I use this one instead of get_permalink() or get_page_by_title():

function get_link_by_page_class($page_class){
global $wpdb;
    //Since the meta SHOULD be unique once per language I could use get_var.
    //But I keep get_results and take the first found page with that meta for programming reasons.
$pages = $wpdb->get_results($wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta m WHERE m.meta_key = 'class' AND m.meta_value= %s", $page_class));

if($pages && isset($pages[0])){
    $p = $pages[0];
    echo get_permalink($p->post_id);
}
}

By assigning the 'class' custom field to the pages of one language and then using these functions in the theme code, I can duplicate the theme for all languages (or maybe in some cases just use the same..) without worring about switching page names in functions that use them.

0

UPDATE (polylang plugin):

Here's a revised version of the original function, this time for use with polylang plugin:

//assume polylang as multilingual plugin
function get_link_by_template($template,$lang_slug = null){
    global $wpdb;
    $joins = "";
    $wh = "";

    if($lang_slug){
        $joins = " INNER JOIN $wpdb->term_relationships tr ON meta.post_id = tr.object_id
        INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN $wpdb->terms t ON tt.term_id = t.term_id ";

        $wh = " AND t.slug = %s";
    }

    $pages = $wpdb->get_results($wpdb->prepare(
        "SELECT post_id
        FROM $wpdb->postmeta meta " . $joins . "
        WHERE meta.meta_key = %s AND meta.meta_value= %s" . $wh,
            '_wp_page_template',
            $template,
            $lang_slug
    ));

    if($pages && isset($pages[0])){
      $p = $pages[0];
      echo get_permalink($p->post_id);
    }

}//get_link_by_template

usage:

//no language
get_link_by_template('contacts.php');

//current language
get_link_by_template('contacts.php',pll_current_language());

//manual language
get_link_by_template('contacts.php','it');

pll_current_language() returns the current language. See polylang codex.


OLD (for wpml):

I add another answer, to solve also the problem of linking. I've made another function to use with the 'is_page_template' way:

 //assume wpml as multilingual plugin
function get_link_by_template($template,$check_lang = false){
    global $wpdb;

    //if there's the need of adding lang vars on the URL
    $lang_join_string = (!$check_lang) ? "" : " left join ". $wpdb->prefix ."icl_translations tr on meta.post_id = tr.element_id ";
    $lang_where_string =  (!$check_lang) ? "" : " AND tr.language_code = '" . ICL_LANGUAGE_CODE . "'";

    $pages = $wpdb->get_results($wpdb->prepare(
        "SELECT post_id 
        FROM $wpdb->postmeta meta" . $lang_join_string . "
        WHERE meta.meta_key = '_wp_page_template' AND meta.meta_value= %s" . $lang_where_string, 
    $template));

    if($pages && isset($pages[0])){
        $p = $pages[0];
        echo get_permalink($p->post_id);
    }
}//get_link_by_template
0

I've found out that another answer could be to just use the is_page_template tag.

Since I'm preatty sure that the NAME of the php template file that needs custom css and/or js will be the same through all the languages (both using the multisite way or things like WPML), I can say

if(is_page_template('that-page.php')) do this..

Simple and fast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.