亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

限制 Wordpress 中現有和新的永久鏈接 slugs 的大小以進行 SEO

限制 Wordpress 中現有和新的永久鏈接 slugs 的大小以進行 SEO

PHP
阿波羅的戰車 2023-10-21 20:00:05
我在 Google 上讀到一篇文章,其中提到為了良好的 SEO,最好將 URL 中的 slug 大小限制為 5 個單詞。當我使用 WordPress 時,鏈接會自動分配給文章標題。要僅用 5 個字重做所有鏈接,我必須花費數月時間來編輯博客上的所有鏈接??梢宰詣訄绦写瞬僮鲉??有一些函數或代碼可以實現這一點。我找到了這段代碼并將其添加到我的主題的功能頁面中,但沒有結果??创a: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 個單詞?
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

首先,請注意是否值得這樣做:

影響 SEO 的因素實際上有數百個。您不需要實施所有這些,而且許多總體上不會產生很大的影響。在我看來,這樣做很可能不會產生任何顯著的效果,而且只會讓事情變得更加困難。

更重要的是,要對 SEO 產生任何影響,?slug 應該包含您的關鍵字,如果您以編程方式更改它們,則無法確保 slug 包含關鍵字,因此您甚至可能弊大于利。

僅供參考,幾個版本前,WP 被更改為對 slugs 實施此限制,然后很快又改回來。這對我來說意味著它可能不是很有用或不太實用。

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

請注意,這僅適用于新的slugs,您仍然需要重新生成舊的 slugs,或者編寫代碼來遍歷現有的 slugs 來更新它們。


更新現有的 slugs


您可以將此函數添加到您的functions.php中以獲取所有slug,調用上面的函數以生成新的slug,然后在數據庫中更新它:


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

? ? ? ? ? ? ? ? )

? ? ? ? ? ? );

? ? ? ? }

? ? }

}

請注意,上面的代碼是我自己的,未經測試,因此請確保先在測試環境中嘗試一下。


如何使用此功能


要更新所有現有的 slugs,您只需按需調用此函數一次,而不是自動調用(否則每次加載 function.php 時它都會更新 slugs)。您可以通過創建單獨的獨立頁面,在 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>";

?>


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號