简体   繁体   English

wordpress在每个帖子上运行javascript代码

[英]wordpress run javascript code on every post

I am developing a website and I created a js script that creates side menu for me. 我正在开发一个网站,并且创建了一个js脚本,可以为我创建侧边菜单。

It reads all the headings and creates scroll menu. 它读取所有标题并创建滚动菜单。

It works perfectly the problem that I am facing now is that I have to call the js function on every post page. 它完美地工作了,我现在面临的问题是我必须在每个帖子页面上调用js函数。 Since I'll have lots of posts on my web page this could be a bit time consuming. 由于我的网页上会有很多帖子,所以这可能会花费一些时间。 Any way to make my script load automatically every time a post is being shown on web page? 有什么方法可以使每次在网页上显示帖子时自动加载我的脚本?

My code if anyone is interested is bellow: 如果有人感兴趣,我的代码如下:

 function create_navigation() { allid = []; //tabela z vsemi id ji, ki morajo biti v meniju elements = document.getElementsByTagName("h1"); for (var i = 0; i < elements.length; i++) { if (elements[i].id != "") { allid.push(elements[i].id) } } elements = document.getElementsByTagName("h2"); for (var i = 0; i < elements.length; i++) { if (elements[i].id != "") { allid.push(elements[i].id) } } for (var i = 0; i < allid.length; i++) { var table = document.getElementById("qweqwe"); var row = table.insertRow(i); var cell1 = row.insertCell(0); cell1.innerHTML = "<a href=\\"#" + allid[i] + "\\" class=\\"_ps2id _mPS2id-h mPS2id-highlight mPS2id-highlight-first mPS2id-highlight-last\\" data-ps2id-offset=\\"\\">" + document.getElementById(allid[i]).innerHTML + "</a>"; } } 

Currently I call the function create_navigation on every post page that I want it to appear. 目前,我希望每个要显示的帖子页面都调用函数create_navigation。

Also is this the correct way of doing this? 这也是这样做的正确方法吗? I am kinda inexperienced in Wordpress and had time coming with better idea of dynamically creating side menu. 我对Wordpress没什么经验,所以有时间更好地动态创建侧边菜单。

The best solution would be to add this code to the themes JS file. 最好的解决方案是将此代码添加到主题JS文件中。 That way it's loaded on every page load. 这样,它会在每次页面加载时加载。

If there isn't a JS file in the theme, just create one: 如果主题中没有JS文件,则只需创建一个:

1.Create file, ie. 1.创建文件,即 scripts.js scripts.js

2.Place it in themefolder/js/ 2.将其放在themefolder / js /中

3.Add this to themes functions.php (located in theme root folder) 3.将此添加到主题functions.php(位于主题根文件夹中)

function theme_scripts() {
    wp_enqueue_script( 'theme-js', get_theme_file_uri( '/js/scripts.js' ), array( 'jquery' ), '1.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_scripts' );

If you use theme which allows adding custom code, just use those options. 如果您使用允许添加自定义代码的主题,则只需使用这些选项。

If not, you can simply use html block on sidebar and add your script there. 如果没有,您可以在边栏上使用html块,然后在其中添加脚本。

Go to http://Your-site.com/wp-admin/widgets.php and add simple HTML widget to sidebar, then add your js inside it and save. 转到http://Your-site.com/wp-admin/widgets.php并将简单的HTML小部件添加到侧边栏,然后在其中添加js并保存。 Make sure your sidebar is visible on every page though. 确保您的侧边栏在每个页面上都可见。 Or just place widget in different location, which is visible on every page. 或者只是将小部件放在不同的位置,这在每个页面上都可见。

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

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