1

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).

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
Adrián Pérez
  • 107
  • 1
  • 12
  • Are you sending _action_ with your AJAX request? Please add your ajax code & its php handler. – Abhineet Verma Jun 25 '14 at 12:34
  • Hi @AbhineetVerma , just added this info to the post – Adrián Pérez Jun 25 '14 at 14:45
  • Did you find what's blocking qtranslate to pick up the `$cat=` arg ? I have a similar issue with a list-of-posts WP-query: `have_posts()) : while($list_of_services->have_posts()) : $list_of_services->the_post();?>` I can't create a loop delimited by the `$cat` tag, with other $args it's working (for ex: `$p=66` ) – maioman Oct 06 '14 at 14:02
  • Hi @maioman, in my case finally it was a problem with the url call, with 'idioma = "http://yoursite.com/wp-admin/admin-ajax.php/?lang=en";' variable per language it worked for me. – Adrián Pérez Oct 06 '14 at 18:00
  • I worked it out setting a categorized_blog & category_transient_flusher function – maioman Oct 06 '14 at 21:13

1 Answers1

0

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

Community
  • 1
  • 1
Abhineet Verma
  • 1,008
  • 8
  • 18
  • Hi Abhineet, thank you for your reply but: A) I have an url that cames from the variable 'idioma' wich load the admin-ajax plus ?lang=en or 'es' depending of the selected language B) I have a data:action that works perfectly with all the content elements, you can check it if you press the 'Load more' button on new.whantonline.org The only problem its that it doens't take the translated category...very strange... – Adrián Pérez Jun 25 '14 at 22:28