如何去掉WordPress分類目錄url鏈接中的category,如何處理生成的作者鏈接


一個新網站需要結合SEO,才能做成一個優秀的網站,

◆◆◆ 關於WordPress的分類目錄url中含有category的處理辦法:

1,如果是新網站這些設置需要提前做,方便以后做SEO

1、修改固定鏈接設置

打開WP后台-設置-固定鏈接

自定義結構,可以依據對SEO友好設置url

溫馨提示:在分類目錄那一欄里寫入英文半角的點,此時得需要你的網站是全新的,沒有分類欄目和文章,這樣才不會出錯,如果已有文章,這樣的方法會使你的文章和分類欄目不存在
2.借助Wordpress插件

使用插件“WP No Category Base”插件

3.WP No category Base 插件的主體代碼插入到function.php中(不使用插件,只放代碼在后台)

//去除分類標志代碼
add_action( 'load-themes.php',  'no_category_base_refresh_rules');
 add_action('created_category', 'no_category_base_refresh_rules');
 add_action('edited_category', 'no_category_base_refresh_rules');
 add_action('delete_category', 'no_category_base_refresh_rules');
 function no_category_base_refresh_rules() {
     global $wp_rewrite;
     $wp_rewrite -> flush_rules();
 }
 // register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
 // function no_category_base_deactivate() {
 //  remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
 //  // We don't want to insert our custom rules again
 //  no_category_base_refresh_rules();
 // }
 // Remove category base
 add_action('init', 'no_category_base_permastruct');
 function no_category_base_permastruct() {
     global $wp_rewrite, $wp_version;
     if (version_compare($wp_version, '3.4', '<')) {
         // For pre-3.4 support
         $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
     } else {
         $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
     }
 }
 // Add our custom category rewrite rules
 add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
 function no_category_base_rewrite_rules($category_rewrite) {
     //var_dump($category_rewrite); // For Debugging
     $category_rewrite = array();
     $categories = get_categories(array('hide_empty' => false));
     foreach ($categories as $category) {
         $category_nicename = $category -> slug;
         if ($category -> parent == $category -> cat_ID)// recursive recursion
             $category -> parent = 0;
         elseif ($category -> parent != 0)
             $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
         $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
         $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
         $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
     }
     // Redirect support from Old Category Base
     global $wp_rewrite;
     $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
     $old_category_base = trim($old_category_base, '/');
     $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
     //var_dump($category_rewrite); // For Debugging
     return $category_rewrite;
 }
 // Add 'category_redirect' query variable
 add_filter('query_vars', 'no_category_base_query_vars');
 function no_category_base_query_vars($public_query_vars) {
     $public_query_vars[] = 'category_redirect';
     return $public_query_vars;
 }
 // Redirect if 'category_redirect' is set
 add_filter('request', 'no_category_base_request');
 function no_category_base_request($query_vars) {
     //print_r($query_vars); // For Debugging
     if (isset($query_vars['category_redirect'])) {
         $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
         status_header(301);
         header("Location: $catlink");
         exit();
     }
     return $query_vars;
 }

 ◆◆◆WordPress會生成很多作者鏈接
   因為wp有一個作者歸檔的模板文件

  wordpress作者的相關函數調用代碼

<?php the_author(); ?> 顯示文章的作者  
2.<?php the_author_description(); ?> 顯示文章作者的描述(作者個人資料中的描述)  
3.<?php the_author_login(); ?> 顯示文章作者的登錄名  
4.<?php the_author_firstname(); ?> 顯示文章作者的firstname(名)  
5.<?php the_author_lastname(); ?> 顯示文章作者的lastname(姓)  
6.<?php the_author_nickname(); ?> 顯示文章作者的昵稱  
7.<?php the_author_ID(); ?> 顯示文章作者的ID號  
8.<?php the_author_email(); ?> 顯示文章作者的電子郵箱  
9.<?php the_author_url(); ?> 顯示文章作者的網站地址  
10.<?php the_author_link (); ?>(添加於2.1版本) 顯示一個以文章作者名為鏈接名,鏈接地址為文章作者的網址的鏈接。  
11.<?php the_author_icq(); ?> (不推薦使用) 顯示文章作者的icq  
12.<?php the_author_aim(); ?> 顯示文章作者的aim  
13.<?php the_author_yim(); ?> 顯示文章作者的yim  
14.<?php the_author_msn(); ?> (不推薦使用) 顯示文章作者的msn  
15.<?php the_author_posts(); ?> 顯示文章作者已發表文章的篇數  
16.<?php the_author_posts_link(); ?> 顯示一個鏈接到文章作者已發表文章列表的鏈接  
17.<?php list_authors(); ?> (不推薦使用) 顯示blog所有作者和他們的相關信息。完整函數如下:  

1. 一個不錯的解決方法是將WordPress作者存檔鏈接中的用戶名改為昵稱。
修改方法如下:

/**
 * 將WordPress作者存檔鏈接中的用戶名改為昵稱
 * https://www.wpdaxue.com/use-nickname-for-author-slug.html
 */
//使用昵稱替換用戶名,通過用戶ID進行查詢
add_filter( 'request', 'wpdaxue_request' );
function wpdaxue_request( $query_vars )
{
    if ( array_key_exists( 'author_name', $query_vars ) ) {
        global $wpdb;
        $author_id = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='nickname' AND meta_value = %s", $query_vars['author_name'] ) );
        if ( $author_id ) {
            $query_vars['author'] = $author_id;
            unset( $query_vars['author_name'] );    
        }
    }
    return $query_vars;
}
 
//使用昵稱替換鏈接中的用戶名
add_filter( 'author_link', 'wpdaxue_author_link', 10, 3 );
function wpdaxue_author_link( $link, $author_id, $author_nicename )
{
    $author_nickname = get_user_meta( $author_id, 'nickname', true );
    if ( $author_nickname ) {
        $link = str_replace( $author_nicename, $author_nickname, $link );
    }
    return $link;
}

如果不希望蜘蛛爬取這些鏈接:給作者鏈接添加nofollow

打開wp-includes/author-template.php
查找‘<a href="%1$s" title="%2$s">%3$s</a>',只需加個nofollow標簽,例如:'<a href="%1$s" title="%2$s" rel="nofollow">%3$s</a>'

2.更改wordpress主題內的function.php文件,在php循環內增加如下代碼:

//給 the_author_post_link 生成的鏈接加上 rel="nofollow"

add_filter('the_author_posts_link','cis_nofollow_the_author_posts_link');

function cis_nofollow_the_author_posts_link ($link) {

return str_replace('<a href=','<a rel="nofollow" href=', $link);

}

更改/移除WordPress作者存檔頁面的前綴“author”

3.可以在根目錄下新建一個作者頁面author.php  

  


  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM