WordPress.org

Ready to get started?Download WordPress

Forums

Add Post Page Number to Title Tag and/or the_title? (6 posts)

  1. iamPariah
    Member
    Posted 8 years ago #

    When using multi-page posts via <!--nextpage-->, I've noticed a large number of people coming from search engines into posts' second, third, or later pages. Given the way WP2 retains the same HTML page title and the_title for all pages of a post, neither search engines nor users immediately recognize that they haven't begun reading with the beginning of the article.

    I have page navigation devices such as the plugin that generates this forums nav bar, but such things appear only at the bottom and, again, fail to identify the the page number in the title or the_title.

    Is there a way to append or prepend the page number of a multi-page article to the HTML page title (e.g. "My Article, Page 2 > My Site") or within the WP the_title tag (e.g. "My Article, Page 2")?

    Thanks for any advice.

  2. tsguitar
    Member
    Posted 8 years ago #

    Well, you can move those page numbers to the top of your article if you want:
    http://codex.wordpress.org/Styling_Page-Links

    It's all about where you put wp_link_pages() in your template.

    What if you exclude certain data from the link pages and show only the current page you're on? Here's the page from the codex:
    http://codex.wordpress.org/Template_Tags/link_pages

    Here's what I'm thinking:
    $pagenumber = link_pages('', '', 'number', '', '', 'Page %', '');

    I'm hoping that would show only the current page number, but it'll be a link. If you do some strip_tags on that variable $pagenumber, would that give you the page number without the link on it? Then you could include that in your title. I don't have any multiple page posts to try this out on. Tell me what happens.

  3. tsguitar
    Member
    Posted 8 years ago #

    I just checked and this does work. Does anyone have an idea of how to strip tags off something like link_pages? strip_tags doesn't work on anything, as this person found out a little while ago.

  4. iamPariah
    Member
    Posted 8 years ago #

    Thanks, TSGuitar.

    Thank you for figuring that out. I appreciate it. I didn't try that method because there were two things I didn't like: First, it isn't adaptable to anywhere outside the loop; you can't, say, use it in the page title. Second, I don't want the current page number linked to itself.

    Instead, I put the subject on the back burner for a while and let my subconscious chew on it. Ultimately, the solution came to me, which I turned into a plugin (I'll release it officially at some point in the near future):

    <?php

    /*
    Plugin Name: PSB Paged Posts Number
    Plugin URI:
    Description: Adds a tag to display the current page number of multi-page posts (for example: in the post title, the page title, or wherever).
    Author: Pariah S. Burke
    Version: 0.1
    Author URI: http://www.iampariah.com
    */

    function psb_pagednumber()

    {
    global $wp_query;

    if ((($page = $wp_query->get("paged")) || ($page = $wp_query->get("page"))) && $page > 1) {
    echo ' PAGE '.$page . '';
    }
    }

    ?>

    With that plugin activated, open single.php and/or index.php, find the following line:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>

    And insert this immediately after it:

    <?php if(function_exists('psb_pagednumber')) { psb_pagednumber(); } ?>

    Now, if the post is NOT multipage, nothing will happen. If the post IS multipaged, the title will be appended with "Page X"--but only AFTER the first page of the post. So, you'll never see "My Post Page 1"; just "My Post Page 2," "My Post Page 3," and so on.

    It's a great cue to readers who come into a post after the first page, which is pretty common with traffic from search engines.

    The plugin call can also be inserted into the HTML page title like so:

    <title><?php if(is_single()) { ?><?php wp_title(''); ?><?php if(function_exists('psb_pagednumber')) { psb_pagednumber(); } ?> — <?php } ?><?php bloginfo('name'); ?></title>

  5. iamPariah
    Member
    Posted 8 years ago #

    By the way, I'd LIKE to be able to make echo ' PAGE '.$page . ''; a variable that can be set by the user when the plugin is called. Specifically, I'd like to let the user choose text to insert before the number (e.g. "[Page "] and the text after the number ("]") as variables.

    In other words, I want the user to be able to call:

    <?php if(function_exists('psb_pagednumber')) { psb_pagednumber('[Page: ', ']' ); } ?>

    I tried doing that, but I couldn't get the code to work in the plugin itself; my PHP knowledge isn't up to the task.

    Could someone please help by modifying my plugin code (above) to account for such variables?

  6. Jonathan Landrum
    Member
    Posted 7 years ago #

    This is exactly what I was looking for. Front page has PR 5, second page has PR 0. Why? Most likely because of same title tags. Thanks for the code.

Topic Closed

This topic has been closed to new replies.

About this Topic