qTranslate slug, get url of post in other language in Ajax call

I’m making a bilingual WordPress site. I want to load posts via AJAX and have a link to the same post, but in the other language. I’m using qTranslate slug and usually I rely on the global $qtranslate_slug variable like so:

$qtranslate_slug->get_current_url($targetLang);

This works fine if you are on post page, but it doesn’t work in an ajax loaded post.

Read More

In my AJAX call, I’m setting the post object this way:

$post = get_post($postId);
setup_postdata($post);

I get all the data from the post, I’m just not sure what function to call to set the $qtranslate_slug object. In the ajax call, $qtranslate_slug->current_url is an empty array.

UPDATE 1

This is probably not the right way, but I called wp() in my ajax call and it set the $qTranslate_slug object. HOWEVER, the links are “ugly” i.e. http://example.com/fr/?id=68
How can i get the pretty url?

UPDATE 2
in functions.php, i have the following function:

function t($fr, $en){
    switch(qtrans_getLanguage()){
        case "fr":
            return $fr;
        default:
            return $en;
    }
}

Here’s my ajax code:

<?php
global $qtranslate_slug;
if(isset($_GET["id"])){
    $siteRoot = dirname(__FILE__) . "/../../../../";
    require_once $siteRoot . "wp-load.php";
    wp();
    $post = get_post($_GET["id"]);
    setup_postdata($post);

    // generate post html. I didn't include it here, because it has nothing to do with the problem.
    ?>
    <script>
        var alternateUrl = "<?php echo $qtranslate_slug->get_current_url(t('en', 'fr')); ?>";
    </script>

<?php } ?>

UPDATE 3

qTranslate gives you a url in the style of: http://example.com/fr/?id=68
If the “id” is changed to “p”, the url will be converted to a pretty url when you visit the page. However, I would still like to know how to get the pretty version to be displayed in the link.

Related posts

Leave a Reply