简体   繁体   English

无法在Wordpress中获取页面名称

[英]Unable to get page name in wordpress

I am customizing a wordpress plugin which is used for popup email subscribing. 我正在自定义用于弹出电子邮件订阅的wordpress插件。 when the user submits email he gets a confirmation email in which iam sending my custom html to him. 当用户提交电子邮件时,他会收到一封确认电子邮件,其中iam将我的自定义html发送给他。 I have five different pages on my web site and every page has that popup. 我的网站上有五个不同的页面,每个页面都有该弹出窗口。 what iam doing is that i want to get the name of my page than according to that i want to send html in email. 我正在做的是,我要获取页面名称,而不是要在电子邮件中发送html。 I did exactly the same thing for other plugin and it worked but in this popup plugin iam unable to get the page name from where it is called. 我对其他插件做了完全相同的操作,但它确实起作用,但是在此弹出插件中,iam无法从调用它的位置获取页面名称。 I have tried following things but failed. 我尝试了以下操作,但失败了。

global $post;      /* this worked perfect on other plugin */
$pagename = $post->post_name;
if($pagename=="page1")
{
 // html page1 //
}
else
{
// html page2 //
}

Just tried this 刚刚尝试了这个

$slug = basename(get_permalink()); 
if($slug=="page1") and so on

Here you go, you need to get title for this, as 在这里,您需要获取title ,因为

$post = get_post( $post );

$pagename = isset( $post->post_title ) ? $post->post_title : '';

Hope it helps. 希望能帮助到你。

The best way is to use get_queried_object . 最好的方法是使用get_queried_object It retrieve the currently-queried object - page, post, taxonomy, whatever... 它检索当前查询的对象-页面,帖子,分类法等等。

You can try this code, its working for me: 您可以尝试以下代码,它对我有用:

$qo = get_queried_object();
if ( 'page' !== $qo->post_type ) {
    //Here you can be sure, that you are in page query, so this is available
    $pagename = $qo->post_name;
}

Please try to use this: 请尝试使用此:

$pagename = get_query_var('pagename');  
if ( !$pagename && $id > 0 ) {  
    // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object  
    $post = $wp_query->get_queried_object();  
    $pagename = $post->post_name;  
}

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

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