简体   繁体   English

将代码添加到Wordpress网站页面

[英]Add code to Wordpress site page

I have this code, attached in JsFiddle and am wondering how i would add it to a single wordpress page? 我有将此代码附加在JsFiddle中,并且想知道如何将其添加到单个wordpress页面中? I have tried a few plugins but they just seem to mess up the formatting and stop the javascript from working. 我尝试了一些插件,但它们似乎弄乱了格式并停止了javascript的工作。 Any help would be greatly appreciated. 任何帮助将不胜感激。 There is a lot of code so i have only added a small snippet here. 有很多代码,所以我在这里只添加了一个小片段。

<div id="top">  
    <div class= "toptext">  
        <span>Bet Type</span>
        <select id = "typeoption"> 
            <option value="0">Qualifying Bet</option> 
        </select>
    </div>

https://jsfiddle.net/yLqwthyr/1/ https://jsfiddle.net/yLqwthyr/1/

You need to modify your jquery code to run your script in noConflict mode. 您需要修改jquery代码以在noConflict模式下运行脚本。

If you plan to add a single js file, you need to enqueue it with wp_enqueue_script(). 如果计划添加单个js文件,则需要使用wp_enqueue_script()将其加入队列。

A possible way to do this, assuming you want to add your form structure to a page (with an ID and permalink) 假设您想将表单结构添加到页面(具有ID和永久链接),这是一种可能的方法

 add_action('wp_enqueue_scripts', 'se_40975160');

 function se_40975160(){
      if(is_page('your-page')){
           wp_enqueue_script('jquery');
           wp_register_script('form-js', PATHTOJS . '/js/your-js.js', array('jquery'));

      }
 }

For the jQuery script: When you enqueue script that is dependent on jQuery, note that the jQuery in WordPress runs in noConflict mode, which means you cannot use the common $ alias. 对于jQuery脚本:当排队依赖jQuery的脚本时,请注意WordPress中的jQuery在noConflict模式下运行,这意味着您不能使用公共$别名。 You must use the full jQuery instead. 您必须改为使用完整的jQuery。 Alternately, place your code using the $ shortcut inside a noConflict wrapper. 或者,使用$快捷方式将代码放入noConflict包装器中。

jQuery( document ).ready( function( $ ) {
// $() will work as an alias for jQuery() inside of this function
[ your code goes here ]
} );

To insert your form structure, you can use add_shortcode() or simply use the visual editor. 要插入表单结构,可以使用add_shortcode()或简单地使用可视编辑器。 I will use a shortcode to handle the form response with php. 我将使用简码来处理php的表单响应。 You can see more details in the shortcode api pages. 您可以在简码api页面中查看更多详细信息。

Step :1 Create Shortcode for the display purpose 步骤:1创建用于显示目的的简码

function mycustomcode(){
    /*Place your code here*/
}
add_shortcode('mycustomcode','mycustomcode');

Step :2 open file where you want to display just use this shortcode, 第2步:打开要显示的文件,只需使用此短代码,

In php filecode, 在php文件代码中,

do_shortcode('mycustomcode');

In WYSWYG (Wordpress Editor), 在WYSWYG(WordPress编辑器)中,

['mycustomcode']

Step :3 步骤:3

En queue you style and script 正在排队给您样式和脚本

To edit page, you should first figure out which template the page used. 要编辑页面,您首先应该弄清楚页面使用了哪个模板。

You could refer the WordPress Template Hierarchy for more detail. 您可以参考WordPress模板层次结构以获得更多详细信息。

Or you could create a page template for specific use, and assign the page template to the page at wp-admin. 或者你可以创建一个页面模板用于特定用途,并在WP-admin指定页面模板页面。

If you still can't find the template to edit, there are plugins like Show Current Template and Display Template Name to help you find it out. 如果仍然找不到要编辑的模板,则可以使用诸如显示当前模板显示模板名称之类的插件来帮助您找到它。

Remember, when you are editing the template files, do it in the child theme. 请记住,在编辑模板文件时,请在子主题中进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM