Unable to get page name in wordpress

I am customizing a wordpress plugin which is used for popup email subscribing. when the user submits email he gets a confirmation email in which iam sending my custom html to him. 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. 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.
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

Related posts

3 comments

  1. Here you go, you need to get title for this, as

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

    Hope it helps.

  2. The best way is to use 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;
    }
    
  3. 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;  
    }
    

Comments are closed.