Categories not working with qtranslate + ajax call on wordpress

I’m using qTranslate on a custom wordpress theme, I have a jQuery ajax call that load the correct ‘admin-ajax.php/?lang=’ value and call the construction on functions.php

When you call the Ajax (check it at new.whatonline.org, the ‘Load more’ button before the footer, if you are in the english version you will have the new thumbs translated, but not the categories).

Read More

I tried different solutions (_e(), select the language before, etc) but I didn’t arrive to fix it, the code I call for functions is

foreach((get_the_category()) as $category) { 
    if (!($category->cat_ID==894) && !($category->cat_ID==895) && !($category->cat_ID==902) && !($category->cat_ID==903) && !($category->category_parent==902)){
        echo '<a href="';
        echo get_category_link( $category->term_id );
        echo '" >';
        echo $category->cat_name;
        echo '</a>, ';
    }
}

It works when the page loads but not with the AJAX call. I also printed the $category value and I saw all the array but there isn’t the translated value, on other contents like ‘the_content’ I can see the <–:en–><–:es–> if I output the original value but not for categories (I didn’t found the structure on the mySQL database) so I guess the problems comes from a first plugin function, no?

PS. The last category is translated because it isn’t a real category and I use _e(XXXX, ‘qtranslate’) hack

Thanks.

UPDATE

Here is the ajax call from jQuery…

jQuery.ajax({
    type: 'POST',
    cache: false,
    url: idioma,
    data: {"action": "listposts", cat : categories, neworder : order, offset : postoffset}
})
.success(function(data) {   
    //Añadimos los nuevos posts a la cola y borramos los anteriores
    $(".results").append(data).show();
    datosact.remove();
    postoffset = postoffset + 20;
    //Vovlemos a poner los textos bien y devolvemos el fondo de carga a su estado inicial
    $('#loadmore').children('a').text( morearticles );
    $('#loading').removeClass('active');
    return false;
});

and the php function for the construction

function listposts() {  
global $post;

$output = '';
$cat_id = $_POST[ 'cat' ];
$orderby  = $_POST[ 'neworder' ];
$offset  = $_POST[ 'offset' ];

$metakey ='';

if ($orderby == "by_views"){
    $orderby = "meta_value_num";
    $metakey = 'views';
}   
    $args = array (
    'category__and'      => $cat_id,
    'meta_key'           => $metakey,
    'post__not_in'       => get_option( 'sticky_posts' ),
    'orderby'            => $orderby,
    'post_status'        => 'publish',
    'posts_per_page'     => 20+$offset
);

$listposts = new WP_Query($args); 
while($listposts->have_posts()) : $listposts->the_post();

    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'listado' );
    $str = get_post_meta($post->ID, 'subcat', true);

    $vistas = get_post_meta( get_the_ID(), 'views', true );

    echo "<article class='posts'><a href='";
    echo the_permalink();
    echo "'><img src='";
    echo $thumb['0'];
    echo "' alt='";
    the_title();
    echo "'><div class='hover_content'><ul><li><span class='icon-eye'><br>";
    echo $vistas;
    echo "</span></li><li><span class='icon-share'><br>";
    echo render_socialcounter();    
    echo "</span></li></ul></div></a><a href=";
    echo the_permalink();
    echo "'><h1>";
    the_title();
    echo "</h1></a><div class='posts-tags'>";

//This function do the loop that I posted before    
what_categories();  


    echo "<a href='tag/";
    what_tags();
    echo "'>";
    echo _e($str, 'qtranslate');

    echo "</a></div><div class='posts-extract'>";

    echo $content = "<p>".new_excerpt(180)."...</p>";
    echo "</div></article>";

endwhile;

die($output);
}

add_action('wp_ajax_listposts', 'listposts');
add_action('wp_ajax_nopriv_listposts', 'listposts'); // not really needed

Related posts

Leave a Reply

1 comment

  1. As per your code, There are two things missing in your code

    1. AJAX url
    2. Action

    These are required values to send via ajax to use WordPress AJAX.

    jQuery.ajax({
        type: 'POST',
        cache: false,
        action: 'listposts',
        url: <?php echo admin_url( 'admin-ajax.php' ) ?>,
        data: {"action": "listposts", cat : categories, neworder : order, offset : postoffset}
    })
    .success(function(data) {   
        //Añadimos los nuevos posts a la cola y borramos los anteriores
        $(".results").append(data).show();
        datosact.remove();
        postoffset = postoffset + 20;
    
        //Vovlemos a poner los textos bien y devolvemos el fondo de carga a su estado inicial
        $('#loadmore').children('a').text( morearticles );
        $('#loading').removeClass('active');
    
        return false;
    });
    

    You can’t call ajax without action parameter because then there will be no way to identify ajax request uniquely.

    Reference

    http://codex.wordpress.org/AJAX_in_Plugins

    Using AJAX in a WordPress plugin