如何动态复制 WP 帖子的标题以聚焦 Yoast SEO 中的关键字字段? [英] How to copy title of WP post dynamically to focus keyword field in Yoast SEO?

查看:32
本文介绍了如何动态复制 WP 帖子的标题以聚焦 Yoast SEO 中的关键字字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的博客中有大约 1000 篇博文.我使用 Yoast SEO Wordpress 插件.我所有的帖子都没有添加焦点关键字.有什么办法可以动态添加帖子的标题以聚焦关键字字段,而不是一一复制粘贴?

I have around 1K posts in my blog. I use Yoast SEO Wordpress Plugin. All my posts doesn't has focus keyword added. Is there any way possible I can add title of the post dynamically to focus keyword field instead of copy paste one by one?

推荐答案

Yoast Focus Keywords 是一个帖子元,你可以创建一个循环遍历所有帖子,然后使用 get_the_title() 更新帖子元;

Yoast Focus Keywords is a post meta, you can create a loop that loops through all the posts and then update the post meta with get_the_title();

示例代码:

$title = get_the_title();
$args = array('posts_per_page' => -1, 'post_type' => 'post');
$posts = new WP_Query($args);

foreach($posts as $post){
    update_post_meta($post->ID, '_yoast_wpseo_focuskw', $title);
}

wp_reset_postdata();

您可以在functions.php 中添加代码或使用代码创建页面模板.

You can add the code inside your functions.php or create a page template with the code.

更新:

创建一个不包含循环的空白页面模板,然后将以下代码添加到文件中并使用该页面模板创建一个页面.

Create a blank page template that doesn't contain a loop, then add the below code into the file and create a page using this page template.

$posts_query = new WP_Query(
        array(
            'post_type' => 'post',
            'posts_per_page' => -1
        )
    );

    while($posts_query->have_posts()) : $posts_query->the_post();
        $title = get_the_title();
        $post_id = get_the_ID();

        update_post_meta($post_id, '_yoast_wpseo_focusk‌​w', $title);

        echo $title . ' Meta Updated<br />';
    endwhile;

使用页面模板访问页面,它应该浏览每个帖子,并在每次浏览帖子时显示TITLE Meta Updated".

Visit the page using the page template and it should go through each post and display "TITLE Meta Updated" each time it goes through a post.

这篇关于如何动态复制 WP 帖子的标题以聚焦 Yoast SEO 中的关键字字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆