2017-02-18 51 views
1

首先,温柔!我是javascript noob!回复WordPress博文in Javascript功能

我已经创建了一个Codepen替换类,并通过javascript对元素单击应用转换。在Codepen环境中,它完美地工作。当我尝试将相同类型的功能转移到WordPress时,问题就出现了。

这里原来的javascript:

$(document).ready(function(){ 
    $("#panel-1 a.get-big").click(function(){ 
    $("#panel-1").addClass("huge-panel"); 
    $("div:not(#panel-1)").removeClass("huge-panel"); 
    $("#panel-1 #the-top").addClass("move-in-top"); 
    $("#panel-1 #the-bottom").addClass("move-in-bottom"); 
    $("div:not(#panel-1 #the-top)").removeClass("move-in-top"); 
    $("div:not(#panel-1 #the-bottom)").removeClass("move-in-bottom"); 
    }); 
    $("#panel-2 a.get-big").click(function(){ 
    $("#panel-2").addClass("huge-panel"); 
    $("div:not(#panel-2)").removeClass("huge-panel"); 
    $("#panel-2 #the-top").addClass("move-in-top"); 
    $("#panel-2 #the-bottom").addClass("move-in-bottom"); 
    $("div:not(#panel-2 #the-top)").removeClass("move-in-top"); 
    $("div:not(#panel-2 #the-bottom)").removeClass("move-in-bottom"); 
    }); 
    $("#panel-3 a.get-big").click(function(){ 
    $("#panel-3").addClass("huge-panel"); 
    $("div:not(#panel-3)").removeClass("huge-panel"); 
    $("#panel-3 #the-top").addClass("move-in-top"); 
    $("#panel-3 #the-bottom").addClass("move-in-bottom"); 
    $("div:not(#panel-3 #the-top)").removeClass("move-in-top"); 
    $("div:not(#panel-3 #the-bottom)").removeClass("move-in-bottom"); 
    }); 
    $("#panel-4 a.get-big").click(function(){ 
    $("#panel-4").addClass("huge-panel"); 
    $("div:not(#panel-4)").removeClass("huge-panel"); 
    $("#panel-4 #the-top").addClass("move-in-top"); 
    $("#panel-4 #the-bottom").addClass("move-in-bottom"); 
    $("div:not(#panel-4 #the-top)").removeClass("move-in-top"); 
    $("div:not(#panel-4 #the-bottom)").removeClass("move-in-bottom"); 
    }); 
    $("#panel-5 a.get-big").click(function(){ 
    $("#panel-5").addClass("huge-panel"); 
    $("div:not(#panel-5)").removeClass("huge-panel"); 
    $("#panel-5 #the-top").addClass("move-in-top"); 
    $("#panel-5 #the-bottom").addClass("move-in-bottom"); 
    $("div:not(#panel-5 #the-top)").removeClass("move-in-top"); 
    $("div:not(#panel-5 #the-bottom)").removeClass("move-in-bottom"); 
    }); 
}); 

这不是优雅,但它是有效的。 Here it is on Codepen

现在的问题和我迷路的地方。我想将其应用于WordPress自定义循环。 JavaScript中唯一需要的部分(我相信)是第一个引用#panel-1的.click(函数)例程。在循环中,我使用php作为div的id来引入post_ID。我需要找出一种方法,通过get_the_ID()调用动态替换javascript内的panel-1的所有实例。从我在这里和网络上的其他地方收集到的所有信息中,我认为这将使用wp_localize_script,但看着这只会让我感到困惑。

任何帮助,将不胜感激!

回答

0

在别的之前,你需要了解php发生在服务器上,并且javascript发生在浏览器中,当时无论是由php烘烤的东西都已经服务完毕。

您可以通过XHR请求(ajax)返回服务器,不一定使用新的页面请求。但是,这将是一个不同的查询。您无法与javascript的WP主查询进行交互。这一切发生在php

为了更好地理解了原理和技术参与其中,你应该阅读这些:


要找到适合您尝试做的任何解决方案,您应该前往Codex