admin管理员组文章数量:1327661
for my google event tracking, I need to have the Page Title in Wordpress of the current site within a jquery function.
There are many ways to get the title with php, but I´m not really shure, that it is the best way.
Thank you, Cheers Marten
for my google event tracking, I need to have the Page Title in Wordpress of the current site within a jquery function.
There are many ways to get the title with php, but I´m not really shure, that it is the best way.
Thank you, Cheers Marten
Share Improve this question edited Dec 14, 2013 at 9:34 user1735856 asked Dec 14, 2013 at 9:23 user1735856user1735856 2817 silver badges16 bronze badges 1- You can easily do this with JavaScript using the code var title = $(document).find("title").text(); – Talha Masood Commented Dec 14, 2013 at 9:34
4 Answers
Reset to default 2Depending on how you generate this page title, you need to pass this through to your JS script.
You can do this with wp_localize_script()
http://codex.wordpress/Function_Reference/wp_localize_script
So after you have enqueued the script you can then pass parameters through.
wp_enqueue_script( 'my-script' );
wp_localize_script( 'my-script', 'script_vars', array('site_title' => 'This is my site title' );
You can then use it in your JS file like this:
alert(script_vars.site_title);
just use:
alert(document.title);
depending on your setting you want to strip out the blog name out of it
First, you'll need to get the title in PHP:
$title = get_the_title();
Include your javascript file:
wp_enqueue_script( 'your-script', '../source/to/your/javascript_file.js' )
Then you need to send the $title variable to your javascript file using wp_localize_script():
wp_localize_script( 'your-script', 'script_vars', array('site_title' => $title );
Now you have access to the site_title in your javascript file by doing so:
var site_title = script_vars.site_title;
console.log(site_title);
Jquery
$(document).find("title").text();
javascript
document.title
or
var sPath=window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
alert(sPage);
本文标签: Get pagetitle in wordpress with jquery or javascriptStack Overflow
版权声明:本文标题:Get pagetitle in wordpress with jquery or javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742230750a2437178.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论