【发布时间】: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 查询有类似的问题:<?php $list_of_services = new WP_Query( 'cat=4'); if ($list_of_services->have_posts()) : while($list_of_services->have_posts()) : $list_of_services->the_post();?>我无法创建由$cat标记分隔的循环,而其他 $args 它正在工作(例如:$p=66)跨度> -
嗨@maioman,在我的情况下,最后是url调用有问题,'idioma = "yoursite.com/wp-admin/admin-ajax.php/?lang=en";'对我有用的每种语言的变量。
-
我设置了分类博客和类别瞬态冲洗功能