WordPress(TwentyTen 主题):无需打开新页面即可获取搜索结果

发布于 2024-11-19 19:45:57 字数 328 浏览 5 评论 0原文

我在 WordPress 网站上工作。

我想使用 AJAX 方法将搜索结果加载到当前打开的页面中。为此,我需要获取执行搜索的 PHP 代码的输出,而无需打开新页面。

通常,在您输入搜索词后,WordPress 会打开 URL 如下所示的新页面:

mydomain.com/?s=searchword

执行搜索的 PHP 代码的输出显示在该页面的 #container div 中。

问题:如何在不打开新页面的情况下获取搜索结果,以便我可以使用 jQuery 将它们加载到当前打开的页面中?

非常感谢您的建议!

I work on a WordPress site.

I want to load search results into the currently open page using AJAX methods. To be able to do that, I need to obtain the output of the PHP code that does the search, without opening the new page.

Normally, after you type a search word, WordPress opens the new page with URL like this:

mydomain.com/?s=searchword

The output of the PHP code that does the searching is shown in the #container div of that page.

Question: how can I get the search results without opening the new page, so that I can load them using jQuery into the page that is currently open?

I would be grateful for your advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-11-26 19:45:57

这个插件几乎可以为你做到这一点。您可能想查看该插件的源代码,看看如何自己利用它。

http://wordpress.org/extend/plugins/twowp-ajax-search/ Screenshots/

我想你也可以自己调用搜索页面,解析出#container值,然后自己解析结果列表。如果您需要帮助,请告诉我。否则我认为已经开发的插件非常方便。

根据这里的要求,我对如何做到这一点有更多的想法。这些都不是测试人员,但我的总体想法是合理的。您必须根据您的安装情况进行适当调整。例如获取与发布。暂停。请求数据。

  1. 使用 jQuery 的 ajax 调用来请求搜索页面。

    <前><代码>$.ajax({
    异步:真,
    数据:$dataToSend,
    数据类型:'xml',
    发送前:函数(){
    console.log('rq');
    },
    错误:函数(jqXHR,textStatus,errorThrown){
    console.log('错误');
    },
    成功:函数(xml,textstatus,jqXHR){
    //处理响应数据
    },
    超时:10000,
    类型:'发布',
    url: 'http://' + document.location.host + '/search'
    });

  2. 处理响应并挑选出您感兴趣的数据。
    http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

    $(xml).find('div[class="post"]').each( function() {
    $("#currentPage").append($(this) + "
    ");
    });

SO 拒绝对该列表位进行代码块。对此感到非常抱歉。

This plugin does it for you pretty much. You might want to look at the source code of the plugin and see how you can leverage it for yourself.

http://wordpress.org/extend/plugins/threewp-ajax-search/screenshots/

I suppose you could also call the search page yourself, parse out the #container value and then parse the results list yourself. If you want help with that let me know. Otherwise I think an already developed plugin is pretty convenient.

As requested here is some more thoughts on how I think I might do this. None of this is tester, but I the general ideas are sound. You will have to tweak things as appropriate to match your installation. For example Get vs Post. Timeout. Request Data.

  1. Use jQuery's ajax call to request the search page.

    $.ajax({
                    async: true,
                    data: $dataToSend,
                    datatype: 'xml',
                    beforeSend: function() {
                        console.log('rq');
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.log('error');
                    },
                    success: function(xml, textstatus, jqXHR) {
                        //Process Response Data
                    },          
                    timeout: 10000,
                    type: 'POST',
                    url: 'http://' + document.location.host + '/search'     
            });
    
  2. Process the response and cull out the data you are interested in.
    http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

    $(xml).find('div[class="post"]').each( function() {
    $("#currentPage").append($(this) + "
    ");
    });

SO refuses to code block that list bit. So sorry about that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文