qTranslate和AJAX的Wordpress问题 [英] Wordpress problems with qTranslate and AJAX

查看:76
本文介绍了qTranslate和AJAX的Wordpress问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有qTranslate和自定义主题的wordpress.主题带来了自动完成搜索.我的问题是帖子/页面标题未翻译.我检查了stackoverflow/google,而AJAX请求和qTranslate似乎有问题.这就是为什么我将以下内容添加到我的php文件的url参数中的原因:

?lang='.qtrans_getLanguage()

我检查了控制台,并将参数正确(对于每种语言)传递给了javascript文件.现在,我需要读取此url参数并将其返回给我的php文件(使用$.getJSON时,我在url中添加了lang=en来测试js文件,并且该文件可以工作,但我需要使用所传递的语言).

PHP

<?php

    add_action( 'init', 'myprefix_autocomplete_init' );  
    function myprefix_autocomplete_init() {  
        // Register our jQuery UI style and our custom javascript file  
        wp_register_script( 'my_acsearch', get_template_directory_uri() . '/nectar/assets/functions/ajax-search/wpss-search-suggest.js?lang='.qtrans_getLanguage(), array('jquery','jquery-ui-autocomplete'),null,true);  
        wp_localize_script( 'my_acsearch', 'MyAcSearch', array('url' => admin_url( 'admin-ajax.php' ))); 

        // Function to fire whenever search form is displayed  
        add_action( 'get_search_form', 'myprefix_autocomplete_search_form' );  

        // Functions to deal with the AJAX request - one for logged in users, the other for non-logged in users.  
        add_action( 'wp_ajax_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
        add_action( 'wp_ajax_nopriv_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
    }  

    function myprefix_autocomplete_search_form(){  
        wp_enqueue_script( 'my_acsearch' );  
    }  

    add_action( 'wp_ajax_{action}', 'my_hooked_function' );  
    add_action( 'wp_ajax_nopriv_{action}', 'my_hooked_function' );  

    function myprefix_autocomplete_suggestions(){  

        $search_term = $_REQUEST['term'];
        $search_term = apply_filters('get_search_query', $search_term);

        $search_array = array(
            's'=> $search_term, 
            'showposts'   => 6,
            'post_type' => 'any', 
            'post_status' => 'publish', 
            'post_password' => '',
            'suppress_filters' => true
        );

        $query = http_build_query($search_array);

        $posts = get_posts( $query );

        // Initialise suggestions array  
        $suggestions=array();  

        global $post;  

        foreach ($posts as $post): setup_postdata($post);  

            $suggestion['lang'] = $_GET['lang'];
            $suggestion['label'] = esc_html(qtrans_use(qtrans_getLanguage(), $post->post_title, true)); 
            $suggestion['link'] = get_permalink();  
            $suggestion['image'] = (has_post_thumbnail( $post->ID )) ? get_the_post_thumbnail($post->ID, 'thumbnail', array('title' => '')) : '<i class="icon-salient-pencil"></i>' ; 

            if(get_post_type($post->ID) == 'post'){

                $suggestion['post_type'] = __('Blog Post',NECTAR_THEME_NAME); 

            } else if(get_post_type($post->ID) == 'page'){

                $suggestion['post_type'] = __('Page',NECTAR_THEME_NAME); 

            } else if(get_post_type($post->ID) == 'portfolio'){

                $suggestion['post_type'] = __('Portfolio Item',NECTAR_THEME_NAME); 

                //show custom thumbnail if in use
                $custom_thumbnail = get_post_meta($post->ID, '_nectar_portfolio_custom_thumbnail', true); 
                if(!empty($custom_thumbnail) ){
                    $attachment_id = pn_get_attachment_id_from_url($custom_thumbnail);
                    $suggestion['image'] = wp_get_attachment_image($attachment_id,'portfolio-widget');
                }

            } else if(get_post_type($post->ID) == 'product'){

                $suggestion['post_type'] = __('Product',NECTAR_THEME_NAME); 
            }

            // Add suggestion to suggestions array  
            $suggestions[]= $suggestion;  
        endforeach;  

        // JSON encode and echo  
        $response = $_GET["callback"] . "(" . json_encode($suggestions) . ")";  
        echo $response;  

        // Don't forget to exit!  
        exit;  
    }   

?>

JS

jQuery(document).ready(function ($){  

    var acs_action = 'myprefix_autocompletesearch';  
    $("#s").autocomplete({ 
        delay: 50,
        position: {of: "#search-outer #search .container" },
        appendTo: $("#search-box"), 
        source: function(req, response){  
            $.getJSON(MyAcSearch.url+'?callback=?&action='+acs_action, req, response);  
        },  
        select: function(event, ui) {  
            window.location.href=ui.item.link;  
        },  
        minLength: 2,  
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li>" )
        .append( "<a>" + item.image + "<span class='title'>" + item.label + "</span><br/><span class='desc'>" + item.post_type + "</span> </a>" )
        .appendTo( ul );
    };  

}); 

我希望有人可以帮助我

解决方案

即使这不是一个很好的解决方案,我还是想到了这一点:

var lang;

$('script').each(function(i, e) {
    var scriptName = $(e).attr('src');

    if(typeof scriptName != 'undefined') {
        if(scriptName.indexOf('wpss-search-suggest') != -1) {
            lang = scriptName.substr(scriptName.lastIndexOf('=')+1, scriptName.length-1);
        }
    }
});  

$.getJSON(MyAcSearch.url+'?callback=?&lang='+lang+'&action='+acs_action, req, response); 

I'm using wordpress with qTranslate and a custom theme. The theme brings along a autocomplete search. My problem is that the post/page titles aren't translated. I checked stackoverflow/google and there seems to be a problem with AJAX requests and qTranslate. That's why I added to the url parameter of my php file the following:

?lang='.qtrans_getLanguage()

I checked the console and the parameter is passed correctly (for each language) to the javascript file. What I need now is to read this url parameter and give it back to my php file (I tested the js file with adding lang=en to the url when using $.getJSON and it worked but I need the passed language).

PHP

<?php

    add_action( 'init', 'myprefix_autocomplete_init' );  
    function myprefix_autocomplete_init() {  
        // Register our jQuery UI style and our custom javascript file  
        wp_register_script( 'my_acsearch', get_template_directory_uri() . '/nectar/assets/functions/ajax-search/wpss-search-suggest.js?lang='.qtrans_getLanguage(), array('jquery','jquery-ui-autocomplete'),null,true);  
        wp_localize_script( 'my_acsearch', 'MyAcSearch', array('url' => admin_url( 'admin-ajax.php' ))); 

        // Function to fire whenever search form is displayed  
        add_action( 'get_search_form', 'myprefix_autocomplete_search_form' );  

        // Functions to deal with the AJAX request - one for logged in users, the other for non-logged in users.  
        add_action( 'wp_ajax_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
        add_action( 'wp_ajax_nopriv_myprefix_autocompletesearch', 'myprefix_autocomplete_suggestions' );  
    }  

    function myprefix_autocomplete_search_form(){  
        wp_enqueue_script( 'my_acsearch' );  
    }  

    add_action( 'wp_ajax_{action}', 'my_hooked_function' );  
    add_action( 'wp_ajax_nopriv_{action}', 'my_hooked_function' );  

    function myprefix_autocomplete_suggestions(){  

        $search_term = $_REQUEST['term'];
        $search_term = apply_filters('get_search_query', $search_term);

        $search_array = array(
            's'=> $search_term, 
            'showposts'   => 6,
            'post_type' => 'any', 
            'post_status' => 'publish', 
            'post_password' => '',
            'suppress_filters' => true
        );

        $query = http_build_query($search_array);

        $posts = get_posts( $query );

        // Initialise suggestions array  
        $suggestions=array();  

        global $post;  

        foreach ($posts as $post): setup_postdata($post);  

            $suggestion['lang'] = $_GET['lang'];
            $suggestion['label'] = esc_html(qtrans_use(qtrans_getLanguage(), $post->post_title, true)); 
            $suggestion['link'] = get_permalink();  
            $suggestion['image'] = (has_post_thumbnail( $post->ID )) ? get_the_post_thumbnail($post->ID, 'thumbnail', array('title' => '')) : '<i class="icon-salient-pencil"></i>' ; 

            if(get_post_type($post->ID) == 'post'){

                $suggestion['post_type'] = __('Blog Post',NECTAR_THEME_NAME); 

            } else if(get_post_type($post->ID) == 'page'){

                $suggestion['post_type'] = __('Page',NECTAR_THEME_NAME); 

            } else if(get_post_type($post->ID) == 'portfolio'){

                $suggestion['post_type'] = __('Portfolio Item',NECTAR_THEME_NAME); 

                //show custom thumbnail if in use
                $custom_thumbnail = get_post_meta($post->ID, '_nectar_portfolio_custom_thumbnail', true); 
                if(!empty($custom_thumbnail) ){
                    $attachment_id = pn_get_attachment_id_from_url($custom_thumbnail);
                    $suggestion['image'] = wp_get_attachment_image($attachment_id,'portfolio-widget');
                }

            } else if(get_post_type($post->ID) == 'product'){

                $suggestion['post_type'] = __('Product',NECTAR_THEME_NAME); 
            }

            // Add suggestion to suggestions array  
            $suggestions[]= $suggestion;  
        endforeach;  

        // JSON encode and echo  
        $response = $_GET["callback"] . "(" . json_encode($suggestions) . ")";  
        echo $response;  

        // Don't forget to exit!  
        exit;  
    }   

?>

JS

jQuery(document).ready(function ($){  

    var acs_action = 'myprefix_autocompletesearch';  
    $("#s").autocomplete({ 
        delay: 50,
        position: {of: "#search-outer #search .container" },
        appendTo: $("#search-box"), 
        source: function(req, response){  
            $.getJSON(MyAcSearch.url+'?callback=?&action='+acs_action, req, response);  
        },  
        select: function(event, ui) {  
            window.location.href=ui.item.link;  
        },  
        minLength: 2,  
    }).data( "ui-autocomplete" )._renderItem = function( ul, item ) {
        return $( "<li>" )
        .append( "<a>" + item.image + "<span class='title'>" + item.label + "</span><br/><span class='desc'>" + item.post_type + "</span> </a>" )
        .appendTo( ul );
    };  

}); 

I hope someone can help me with this

解决方案

Even though it's not a nice solution, I came up with this:

var lang;

$('script').each(function(i, e) {
    var scriptName = $(e).attr('src');

    if(typeof scriptName != 'undefined') {
        if(scriptName.indexOf('wpss-search-suggest') != -1) {
            lang = scriptName.substr(scriptName.lastIndexOf('=')+1, scriptName.length-1);
        }
    }
});  

$.getJSON(MyAcSearch.url+'?callback=?&lang='+lang+'&action='+acs_action, req, response); 

这篇关于qTranslate和AJAX的Wordpress问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆