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>";
?>
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報