• I am writing a plugin that records the page title when a page or post is viewed.

    It appears the loop runs for each sub-page of a page (e.g. blog page) and I’m getting multiple titles. (If there are three sub-pages, I get three titles).

    How do I get only the title of the page that is displayed to the site visitor? If they view the second sub-page, I want that title only, etc. — i.e. I want what is inside the HTML header <title>page name</title> tags.

    I’ve tried different hooks into the loop and different code to retrieve the page title. I now think I need to do this

      outside

    the loop but don’t know how

    Relevant portion of my code:

    <?php
    //add_action('init', 'wlog');
    //add_action('wp_head', 'wlog');
    add_action('wp', 'wlog');
    //add_action('wp_footer', 'wlog');
    
    $page_name = wp_title('', false);

    Any help would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The page title within <title> tags does not always relate to the title found in the loop. It only does so for single pages, any archive type of page will have a title only remotely related to loop content. What is actually placed within <title> tags is theme dependent, but the return of wp_get_document_title() is almost universally used.

    • This reply was modified 7 years, 1 month ago by bcworkz.
    Thread Starter tcloud

    (@tcloud)

    thanks, I’ll look into that

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘get title of current page’ is closed to new replies.