简体   繁体   English

特定页面中的自定义 Javascript 代码

[英]Custom Javascript code in a specific page

I would need to add a javascript code within a specific page of my wordpress site.我需要在我的 wordpress 网站的特定页面中添加一个 javascript 代码。 I would like to find a solution via plugin, otherwise maybe I could modify the code I already use specifying the page, but I don't know how to do it我想通过插件找到解决方案,否则也许我可以修改我已经使用的代码来指定页面,但我不知道该怎么做

This is my code:这是我的代码:

( function( $ )  {

    $(document).ready( function() {
        $('.title_subtitle_holder_inner > h1:nth-child(1) > span:nth-child(1)').text('EVENTI');
    });

})( jQuery );

I would need that text to be edited on a specific page, in my case the page id would be 217我需要在特定页面上编辑该文本,在我的情况下,页面 ID 将是 217

Thank you谢谢

Probably no harm in injecting that minimal code into all pages, just make it so that it selects elements only when on your specific target page.将最少的代码注入所有页面可能没有坏处,只需使其仅在您的特定目标页面上选择元素即可。

WordPress creates a class page-id-XY on the body element automatically, so just prepend your selector accordingly, so that it will only select elements on that specific page: WordPress 会自动在body元素上创建一个类page-id-XY ,因此只需相应地添加您的选择器,以便它只会选择该特定页面上的元素:

$('body.page-id-217 .title_subtitle_holder_inner > h1:nth-child(1) > span:nth-child(1)')

You can try to use this:你可以尝试使用这个:

https://wordpress.org/plugins/page-specific-scripts/ https://wordpress.org/plugins/page-specific-scripts/

Or any other method for custom scripts on specific page.或特定页面上的自定义脚本的任何其他方法。 From what I recall I think WordPress has this functionality built in.据我回忆,我认为 WordPress 内置了此功能。

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

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