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

如何解决限制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>";
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-