简体   繁体   English

要求JQuery在Wordpress中显示当前页面的标题

[英]Asking JQuery to Display the Title of the Current Page in Wordpress

I have a mobile drop-down/selector menu in my responsive Wordpress theme and I'd like the default displayed option to always be the current page title. 我的响应式Wordpress主题中有一个移动下拉菜单/选择器菜单,我希望默认显示的选项始终是当前页面标题。 I've found where to change the default text (currently 'Menu'), but I don't know how to make it be the current page title. 我已经找到了更改默认文本(当前为“菜单”)的位置,但是我不知道如何使它成为当前页面标题。

This post seems to have a similar problem, but I can't seem to adapt it to my situation, as I'm a beginnger to JQuery > ( append wordpress post title with jquery ) 这篇文章似乎有一个类似的问题,但是我似乎无法使其适应我的情况,因为我是JQuery的初学者>( 用jquery附加wordpress文章标题

Here's my code. 这是我的代码。 I want to put something like <?php the_title(); ?> 我想输入<?php the_title(); ?> <?php the_title(); ?> where it currently says Menu, but I know PHP and JQuery don't talk to each other. <?php the_title(); ?>当前显示菜单,但我知道PHP和JQuery不会互相交谈。

    $nav_params = array(
    'text' => __('Menu','wpex'),
);
wp_localize_script( 'wpex-responsive', 'responsiveLocalize', $nav_params );

.

This is my workaround since I can't figure out how to have the selector automatically be tuned to the current page in the menu, without any "default" 'Menu' or other option at the top of the menu. 这是我的解决方法,因为我无法弄清楚如何在菜单顶部没有任何“默认”“菜单”或其他选项的情况下自动将选择器调整到菜单中的当前页面。 I could share more code if anybody has advice with that instead. 如果有人对此提出建议,我可以共享更多代码。

Thanks for the help! 谢谢您的帮助!

The article title is in some heading element, right? 文章标题位于某些标题元素中,对吗?

var myTitle = $('h1.myHeadingClass').text();

$nav_params = array(
    'text' => __(myTitle,'wpex'),
);
wp_localize_script( 'wpex-responsive', 'responsiveLocalize', $nav_params );

If that isn't an option for whatever reason, do like the other answer suggested and place the title in a hidden element that you can grab later: 如果由于某种原因这不是一个选择,请按照建议的其他答案进行操作,然后将标题放在隐藏的元素中,以便以后使用:

<div id="myTitle" style="position: absolute; left: -999em;" aria-hidden="true">
    <?php the_title(); ?>
</div>

var myTitle = $('#myTitle').text();
...

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

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