点击只有当执行PHP函数(字preSS) [英] Execute php function only when clicked (wordpress)

查看:150
本文介绍了点击只有当执行PHP函数(字preSS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的话,preSS网站的PHP文件中的一个,我有以下的code:

For one of my wordpress site php files, I have the following code:

  <div class="tabs">
      <a href="#tab1" id="items_id">Items</a>
  </div>
  <div class="section" id="tab1">
    <?php get_template_part('page/tab1'); ?>            
  </div>

那么,它会调用 tab1.php 文件到 DIV ID =tab1的部分。

不过,我希望把它使 get_template_part 仅执行或调用的时候,产品选项卡点击。

However, I want to make it so that the get_template_part is only executed or called when the Items tab is clicked.

什么是jQuery的来电或执行 get_template_part 功能?

What would be the jQuery to call or execute the get_template_part function?

编辑:

那么,我想实现的是类似阿贾克斯。因为 get_template_part 函数将不会被调用,直到项目设置页点击,浏览器不必拨打不必要的文件,并减慢页面。

So, what I am trying to achieve is similar to Ajax. Since the get_template_part function won't be called till the "Items"tab is clicked, the browser does not have to call unnecessary files and slow down the page.

让我知道,如果你认为这是做到这一点的最好办法。

Let me know if you think this is the best way to do it.

谢谢!

推荐答案

虽然后面是已经在他的回答说明拉斐尔的想法,我进行干预,以添加一些细节。

Though the idea behind is already illustrated by Raphael in his answer, I intervene to add some details.

使用AJAX与Word preSS的最好方法是使用其内置的处理它的方式,并通过将请求发送到可湿性粉剂管理员/管理员-ajax.php (我知道文件名的admin的一部分,是一个有点误导,但在前端的所有请求(观看侧),以及管理员可以在联系-ajax.php ,有很多好处,尤其是对安全性。而对于将要执行的服务器端code PHP,应该放在的functions.php

the best way to use AJAX with Wordpress is to use its built-in way of handling it, and that by sending requests to wp-admin/admin-ajax.php( I know the "admin" part of the file name is a bit misleading. but all requests in the front-end (the viewing side) as well as the admin can be processed in admin-ajax.php, with a lot of benefits, especially for security. And for the server-side code php that will be executed, it should be placed in functions.php.

您的jQuery code看起来是这样的:

Your jQuery code will look something like:

 $(document).ready(function() {
        $('.tabs a').click(function(e) {
            e.preventDefault();

            var tab_id = $(this).attr('id'); 


            $.ajax({
                type: "GET",
                url: "wp-admin/admin-ajax.php", 
                dataType: 'html',
                data: ({ action: 'yourFunction', id: tab_id}),
                success: function(data){
                          $('#tab'+tab_id).html(data);
                },
                error: function(data)  
                {  
                alert("Error!");
                return false;
                }  

            }); 

    }); 
}); 

的functions.php 你的主题(或者直接在你的插件文件),地址:

infunctions.php of your theme (or directly in your plugin file), add:

add_action('wp_ajax_yourFunction', 'yourFunction');
add_action('wp_ajax_nopriv_yourFunction', 'yourFunction');

和定义在同一文件中 yourFunction 这样的回调函数:

and define in the same file yourFunction callback function like this:

    function yourFunction() {

// get id
    // your php code

    die();

    }

有关的JavaScript部分,看看阿贾克斯()及其简写<一个HREF =htt​​ps://api.jquery.com/jquery.get/相对=nofollow>获得()。而对于使用AJAX与Word preSS的最佳实践,也有在网络上很多教程(我会回来给一个)。祝你好运

For the javascript part, take a look at ajax() and its shorthand get(). And for the best practices using AJAX with Wordpress, there are many tutorials on the web (I will be back to give one). Good luck

编辑:

因为它是由卡尔mentionned,您可以使用 .load()而不是阿贾克斯(),它应该指出的是 .load()只是一个包装 $。阿贾克斯()。它增加了功能,它允许你定义在文档中返回的数据将被插入。所以真的只可用在呼叫只会导致HTML。这就是所谓的略有不同的其他,因为它是依赖于特定的jQuery包裹DOM元素的方法。因此,人们会做: $('#divWantingContent')负载(...)其内部调用阿贾克斯()

As it is mentionned by Karl, you can use .load() instead of ajax(), It should be noted that .load() is just a wrapper for $.ajax(). It adds functionality which allows you to define where in the document the returned data is to be inserted. Therefore really only usable when the call only will result in HTML. It is called slightly differently than the other as it is a method tied to a particular jQuery-wrapped DOM element. Therefore, one would do: $('#divWantingContent').load(...) which internally calls .ajax().

但我原来的答复是如何组织的PHP code尊重字preSS <一href="http://word$p$pss.stackexchange.com/questions/9231/whats-the-$p$pferred-method-of-writing-ajax-enabled-plugins?rq=1">best实践的。

But my original answer is on how to organize php code respecting Wordpress best practices.

这篇关于点击只有当执行PHP函数(字preSS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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