限制Wordpress for SEO中现有和新的永久链接条的大小

问题描述

我在Google上读过一篇文章,其中说要获得良好的SEO,最好将URL中的条块大小限制为5个字。

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

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

查看代码:

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,$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',99);

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

如何使用它将Wordpress插件的大小限制为5个单词?

解决方法

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

实际上,有数百个因素会影响SEO。您不希望实现所有这些,并且总体上不会有很大的影响。我认为,这样做很可能不会产生任何重大影响,只会使您的工作更加困难。

更重要的是,要对SEO产生任何影响,该标签应包含您的关键字,并且,如果您以编程方式进行更改,则无法确保该标签中包含该关键字 >,因此您甚至可能弊大于利。

仅供参考,几个版本之前,更改了WP以对子弹实施此限制,然后很快将其改回。对我来说,这可能不是很有用或不实用。

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

限制新词条中的单词

您问题中的代码来自本文,对吧?: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,$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_parent));
                $suffix++;
            } while ($post_name_check);
            $slug = $alt_post_name;
        }
    }

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

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

更新现有子弹

您可以将此函数添加到functions.php中,以获取所有子段,调用上述函数以生成新的块,然后在数据库中对其进行更新:

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
                )
            );
        }
    }
}

请注意,上面的代码是我自己的并且未经测试,因此请确保您首先在测试环境中进行尝试。

如何使用此功能

要更新所有现有的子段,您只需要按需调用此函数一次,而不是自动调用(否则,它将在您的functions.php每次加载时更新该子段)。您可以通过创建单独的独立页面,在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>";
?>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...