Login

WordPress SEO: How to dynamically modify meta title?

Author:neo yang Time:2024/02/09 Read: 8189
The meta title, which is the content of the title tag in the head tag of the page, is very important for SEO. Sometimes we need to […]

The meta title, which is the content of the title tag in the head tag of the page, is very important for SEO. Sometimes, we need to dynamically modify WordPress meta title.

Step one: theme support: add_theme_support('title-tag');

Make sure you have this line of code in your theme's functions file:

 add_theme_support('title-tag');

Step 2: Delete the default Meta title: remove_action( 'wp_head', '_wp_render_title_tag', 1 );

Add the following code at the beginning of the page template file where the meta title needs to be dynamically modified:

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

Note: Do not put this line of code in any function.

Step 3: Inject custom Meta title

In the page template file that needs to dynamically modify the Meta title, after deleting the default Meta title code, add the following code:

function custom_title() { global $post; echo " " . $post->post_title . "-"; echo "xxx -"; _e(bloginfo('name'), 'themoon'); echo " "; } add_action("wp_head", "custom_title");

Summarize

The key is to remove the default Meta title. Because, if not deleted, two Meta titles will appear in the end.

tags: ,


copyright © www.lyustu.com all rights reserved.
Theme: TheMoon V3.0. Author:neo yang