admin管理员组

文章数量:1126334

I use the nextpage tag on some of my posts in WordPress. Example: I write two different language versions of a post, write it both down in one post and cut it into two pages with the tag, so everyone can just view and share their language version. When someone shares the second page , the link preview on social media is showing as ‘THE NAME OF THE POST<<PAGE2’ which is not very pleasant for the viewers and looks strange, to be honest. As I use this ‘cutting’ method for language switching at the moment only, how do I rename this ‘Page 2’ text to ‘English version’ , for example, or remove it entirely? Is it possible?

I use the nextpage tag on some of my posts in WordPress. Example: I write two different language versions of a post, write it both down in one post and cut it into two pages with the tag, so everyone can just view and share their language version. When someone shares the second page , the link preview on social media is showing as ‘THE NAME OF THE POST<<PAGE2’ which is not very pleasant for the viewers and looks strange, to be honest. As I use this ‘cutting’ method for language switching at the moment only, how do I rename this ‘Page 2’ text to ‘English version’ , for example, or remove it entirely? Is it possible?

Share Improve this question asked Jan 19, 2024 at 16:37 shanshanarshanshanar 1
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, it is possible to modify the title format for paginated posts in WordPress, including changing or removing the "Page 2" text that appears in link previews on social media. To do this, you'll need to add some custom code to your theme's functions.php file or a site-specific plugin.

The title format for paginated posts is typically controlled by the wp_title filter or document_title_parts filter in newer versions of WordPress. You can hook into this filter to modify the title based on the page number.

Here's an example of how you might do this:

Using the document_title_parts Filter: This filter is used in newer WordPress versions and is more flexible.

function wpb_customize_page_title( $title ) {
    if ( is_singular() && is_paged() ) {
        $page = get_query_var( 'page' );
        if ( $page == 2 ) {
            // Change the title for page 2
            $title['title'] = $title['title'] . ' - English Version';
        } else {
            // Optional: remove page number for other pages
            unset($title['page']);
        }
    }
    return $title;
}
add_filter( 'document_title_parts', 'wpb_customize_page_title', 10, 1 );

Using the wp_title Filter: If you're using an older version of WordPress that doesn't support document_title_parts, you can use the wp_title filter.

function wpb_customize_old_wp_title( $title, $sep ) {
    if ( is_singular() && is_paged() ) {
        $page = get_query_var( 'page' );
        if ( $page == 2 ) {
            // Change the title for page 2
            return 'English Version ' . $sep . ' ' . $title;
        }
    }
    return $title;
}
add_filter( 'wp_title', 'wpb_customize_old_wp_title', 10, 2 );

Remember to replace 'English Version' with whatever text you prefer, and adjust the conditions as needed for your specific setup. This code will alter the title for the second page of paginated posts. If you have different requirements for other pages or other languages, you may need to expand the conditions in the code.

Important Note: Always back up your site before making changes to the code, and ideally, test the changes in a staging environment first. If you're not comfortable editing theme files, you might want to seek assistance from a developer.

本文标签: Changing Nextpage’s Preview