【问题标题】:Categories not working with qtranslate + ajax call on wordpress类别不适用于 qtranslate + ajax 在 wordpress 上调用
【发布时间】:2014-08-15 22:02:08
【问题描述】:

我在自定义 wordpress 主题上使用 qTranslate,我有一个 jQuery ajax 调用,它加载正确的 'admin-ajax.php/?lang=' 值并调用 functions.php 上的构造

当您调用 Ajax 时(在 new.whatonline.org 上查看,页脚前的“加载更多”按钮,如果您使用的是英文版,您将翻译新的拇指,但不翻译类别)。

我尝试了不同的解决方案(_e()、选择之前的语言等)但我没有来修复它,我调用函数的代码是

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>, ';
    }
}

它在页面加载时有效,但在 AJAX 调用时无效。我还打印了 $category 值,我看到了所有数组,但没有翻译后的值,在 'the_content' 等其他内容上,我可以看到 if我输出了原始值,但没有输出类别(我没有在 mySQL 数据库中找到结构)所以我猜问题来自第一个插件函数,不是吗?

PS。最后一个类别被翻译,因为它不是真正的类别,我使用 _e(XXXX, 'qtranslate') hack

谢谢。



更新

这是来自 jQuery 的 ajax 调用...

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;
});

以及用于构建的php函数

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

【问题讨论】:

  • 您是否通过 AJAX 请求发送 action?请添加您的 ajax 代码及其 php 处理程序。
  • 嗨@AbhineetVerma,刚刚将此信息添加到帖子中
  • 您找到阻止 qtranslate 获取 $cat= arg 的原因了吗?我对帖子列表 WP 查询有类似的问题:&lt;?php $list_of_services = new WP_Query( 'cat=4'); if ($list_of_services-&gt;have_posts()) : while($list_of_services-&gt;have_posts()) : $list_of_services-&gt;the_post();?&gt; 我无法创建由$cat 标记分隔的循环,而其他 $args 它正在工作(例如:$p=66)跨度>
  • 嗨@maioman,在我的情况下,最后是url调用有问题,'idioma = "yoursite.com/wp-admin/admin-ajax.php/?lang=en";'对我有用的每种语言的变量。
  • 我设置了分类博客和类别瞬态冲洗功能

标签: php jquery ajax wordpress


【解决方案1】:

根据您的代码,您的代码中缺少两件事

  1. AJAX 网址
  2. 动作

这些是通过 ajax 发送以使用 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;
});

你不能在没有 action 参数的情况下调用 ajax,因为这样就无法唯一地识别 ajax 请求。

参考

http://codex.wordpress.org/AJAX_in_Plugins

Using AJAX in a WordPress plugin

【讨论】:

  • 嗨 Abhineet,谢谢您的回复,但是:A) 我有一个来自变量 'idioma' 的 url,它加载 admin-ajax 加上 ?lang=en 或 'es' 取决于选择的语言 B)我有一个数据:动作与所有内容元素完美配合,如果你按下 new.whantonline.org 上的“加载更多”按钮,你可以检查它唯一的问题是它不接受翻译类别...很奇怪...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多