dongtangze6393 2011-07-14 15:07
浏览 39
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • doufeikuang7724 2011-07-14 17:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向