限制 Wordpress 中现有和新的永久链接 slug 的大小以进行 SEO

Limit the size of the existing and new permalink slugs in Wordpress for SEO

我在 Google 上读到一篇文章说,为了获得良好的 SEO,最好将 URL 中的 slug 的大小限制在 5 个字以内。

当我使用 WordPress 时,link 会自动分配给文章标题。要仅用 5 个单词重做所有 link,我将不得不花费数月时间编辑我博客上的所有 link。

是否可以自动执行此操作?有一些功能或代码。我找到了这段代码并添加到我的主题的功能页面,但没有结果。

见代码:

function pm_limit_slugs_length($uri) {
    $max_words = 5; // If any part of URI contains more than 5 words, the slug will be limited to first 5 words
    $new_title = '';
    $slugs = explode('/', $uri);
    
    for($i=0, $count = count($slugs); $i < $count; $i++) {
        $slug = $slugs[$i];
        $words = explode('-', $slug);
        
        $new_title .= "/";
        if(count($words) > $max_words) {
            $new_title .= implode("-", array_slice($words, 0, $max_words));
        } else {
            $new_title .= $slug;
        }
    }
    
    // Remove trailing slashes
    $new_title = trim($new_title, "/");
    
    return $new_title;
}
add_filter('permalink_manager_filter_default_post_uri', 'pm_limit_slugs_length', 99);
add_filter('permalink_manager_filter_default_term_uri', 'pm_limit_slugs_length', 99);

我在这里找到代码:https://permalinkmanager.pro/docs/filters-hooks/how-to-limit-the-number-of-words-in-wordpress-permalinks/

我如何使用它将 Wordpress slug 大小限制为 5 个单词?

首先,请注意是否值得这样做:

影响 SEO 的因素有 100 多种。你不需要实施所有这些,而且很多不会对整体产生很大的影响。在我看来,这样做很可能不会有什么明显的效果,只会让你更难受。

更重要的是,要对 SEO 产生任何影响 slug 应该包含您的关键字,如果您以编程方式更改它们,无法确保slug 将包含关键字,因此您可能弊大于利。

仅供参考,几个版本前,WP 被更改为对 slug 实施此限制,然后很快又改回原样。这对我来说可能不是很有用或实用。

但是,如果您仍然想这样做:

限制新 slug 中的单词

你问题中的代码是这篇文章里的吧?:How to limit the number of words in WordPress permalinks or slugs?

您使用的第一个示例是与他们的插件一起使用的。下一个示例(包含在下面)将在没有插件的情况下在 Wordpress 中运行。这可以添加到您的 functions.php.

更新:我已将 Automatically Remove Short Words From URL 的代码合并到此函数中以删除少于 3 个字符的短词,请参阅下面的更新函数。

<?php  
/**
 * Trim native slugs
 */
function pm_trim_native_slug($slug, $post_ID, $post_status, $post_type, $post_parent) {
    global $wpdb;

    $max_words = 5; // Limit the number of words to 5; This value can be changed.
    $words = explode('-', $slug);

    /* UPDATED CODE TO REMOVE SHORT WORDS */
    $min_word_length = 2;

    foreach ($words as $k => $word) {
        if (strlen($word) <= $min_word_length)
            unset($words[$k]);
    }
    /* END OF UPDATED CODE FOR SHORT WORDS */

    if(count($words) > $max_words) {
        $slug = implode("-", array_slice($words, 0, $max_words));

        // Make the slugs unique
        $check_sql       = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
        $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $slug, $post_ID));

        if($post_name_check) {
            $suffix = 2;
            do {
                $alt_post_name = _truncate_post_slug($slug, 200 - (strlen($suffix) + 1)) . "-$suffix";
                $post_name_check = $wpdb->get_var($wpdb->prepare($check_sql, $alt_post_name, $post_type, $post_ID, $post_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }

    return $slug;
}
add_filter('wp_unique_post_slug', 'pm_trim_native_slug', 99, 5);

请注意,这仅适用于 slug,您仍然需要重新生成旧的 slug,或编写代码遍历现有的 slug 以更新它们。

更新现有的 slug

您可以将此函数添加到您的 functions.php 以获取所有 slug,调用上面的函数生成一个新的 slug,然后在 DB 中更新它:

function limit_all_existing_slugs(){
    // get all posts
    $posts = get_posts( array (  'numberposts' => -1 ) );
    
    foreach ( $posts as $post ){

        // create the new slug using the pm_trim_native_slug function 
        $new_slug = pm_trim_native_slug($post->post_name, 
                                        $post->ID, 
                                        $post->post_status, 
                                        $post->post_type, 
                                        $post->post_parent);

        // only do the update if the new slug is different 
        if ( $post->post_name != $new_slug ){
            wp_update_post(
                array (
                    'ID'        => $post->ID,
                    'post_name' => $new_slug
                )
            );
        }
    }
}

请注意,上面的代码是我自己的,未经测试,因此请务必先在测试环境中试用。

如何使用这个功能

要更新所有现有的 slug,您只想按需调用此函数一次,而不是自动调用(否则它会在每次 functions.php 加载时更新 slug)。您可以通过创建单独的独立页面在 WP 外部的外部脚本中执行此操作,如下所示:

<?php
    include('wp-load.php');           //Include the wp-load.php file
    define('WP_USE_THEMES', false);   //We don't need the theme files 

    echo "<p>About to update all slugs...</p>";
    limit_all_existing_slugs();
    echo "<p>...DONE</p>";
?>