WordPress优化之去掉分类链接中的category

首页 WordPress优化之去掉分类链接中的category
「WordPress从入门到放弃」QQ交流群:772774006(资源和技术交流)

在网站SEO优化中url优化是相对来说比较重要的环节,wordpress分类的链接地址在wordpress程序中会自带category目录,例如:https://www.wpjc.net/category/learn。

如果能够去掉分类链接中的category无疑也是网站seo优化的一种手法。

下面分享一段去掉分类链接中的category的wordpress代码

使用方法

复制下面代码粘贴到functions.php文件里即可

//去掉分类链接中的category
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;
}

代码很长,也不用看懂,直接将上面代码添加到主题functions.php文件就行

打赏

鼓励作者

您的支持就是我最大的动力!(维持服务器费用)

标题:WordPress优化之去掉分类链接中的category

分类:WordPress优化

链接:https://www.wpjc.net/164.html

版权:wpToo教程网(wpToo.com)所分享发布内容,部分为网络转载,如有侵权请立即联系我们,我们第一时间删除并致歉!

评论

Your email address will not be published. Required fields are marked *

  • Renate
    回复

    This page definitely has all of the info I needed about this subject and didn't know who
    to ask.

    Check out my blog post: google

  • Dawn
    回复

    Hi! Quick question that's totally off topic. Do you know
    how to make your site mobile friendly? My site looks
    weird when browsing from my apple iphone. I'm trying to find a template or plugin that might be
    able to fix this problem. If you have any suggestions, please share.
    Cheers!

    my website :: Daily amazon coupon

  • Linwood
    回复

    Hi there! I know this is kinda off topic but I was wondering if you knew where I
    could locate a captcha plugin for my comment form?
    I'm using the same blog platform as yours and I'm having difficulty
    finding one? Thanks a lot!

    Feel free to visit my website - Work online from home

  • Dieter
    回复

    Because the admin of this website is working, no
    hesitation very soon it will be renowned, due to its feature contents.

    my web blog :: Nike shoebox review - Remona -

  • Douglas
    回复

    you're really a just right webmaster. The site loading
    pace is amazing. It sort of feels that you're doing any distinctive trick.

    Moreover, The contents are masterwork. you've performed a fantastic task in this topic!

    My page; Buy Weed Online Mississauga

  • Tamara
    回复

    Hi! I'm at work browsing your blog from my new iphone
    3gs! Just wanted to say I love reading your blog and look forward
    to all your posts! Keep up the superb work!

    my site ... Buy Weed Online Hillsburgh

  • Salina
    回复

    Hi there, I desire to subscribe for this webpage to get newest updates, so where can i do it please help.

    Also visit my web site ... Web Design company Barrie

  • Bernadette
    回复

    Great post. I'm dealing with many of these issues as well..

    Here is my blog; Halskette

  • Rgfjfw
    回复

    cheap sildenafil 50mg uk - viagra receta buy female viagra tablet in india

  • Ken
    回复

    WOW just what I was searching for. Came here by searching for Star map gift

    my webpage ... Personalized star map

  • Sally
    回复

    hey there and thank you for your info – I've definitely picked up anything
    new from right here. I did however expertise several technical issues using this site, since I experienced to reload the site a lot of times previous to I could
    get it to load properly. I had been wondering
    if your web hosting is OK? Not that I'm complaining, but slow loading instances times will often affect your placement in google and can damage your quality score if advertising and
    marketing with Adwords. Anyway I'm adding
    this RSS to my e-mail and can look out for much more of your respective intriguing content.

    Make sure you update this again very soon.

    My homepage: Monthly subscription service for new parents

  • Roseanne
    回复

    This design is wicked! You obviously know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost...HaHa!) Excellent job.
    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

    My blog; Dog boarding playa vista

  • Lesley
    回复

    Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point.
    You definitely know what youre talking about, why throw away your intelligence on just posting videos to your site when you could be giving us something informative to read?

    Here is my site ... 90 Day Fiance News

  • Ginger
    回复

    Does your blog have a contact page? I'm having
    trouble locating it but, I'd like to shoot you
    an email. I've got some ideas for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it grow
    over time.

    Here is my webpage ... Cash for junk cars

  • Regena
    回复

    When someone writes an article he/she keeps the thought of a user in his/her mind that how a user can be aware of it.
    Thus that's why this post is amazing. Thanks!

    my web blog - SlabZone

  • Louis
    回复

    Appreciating the time and energy you put into your website and in depth information you present.
    It's good to come across a blog every once in a while that isn't the same old rehashed
    information. Fantastic read! I've saved your site and I'm adding your RSS feeds
    to my Google account.

    Also visit my blog post :: embroidery in houston (Genevieve)

  • Heath
    回复

    This is a good tip particularly to those new to the blogosphere.
    Short but very precise information… Thank you for sharing
    this one. A must read article!

    Feel free to visit my website สล็อตเครดิตฟรี,
    Britney,

  • Roseanne
    回复

    There is definately a great deal to know about this subject.

    I like all the points you have made.

    My web site ... guide to malaga

  • Pansy
    回复

    I could not resist commenting. Exceptionally well written!

    My homepage - Socken mit foto

  • Drew
    回复

    Hello there! This blog post could not be written much better!
    Reading through this article reminds me of my previous
    roommate! He continually kept talking about this.
    I'll send this post to him. Fairly certain he will have a good read.
    Thank you for sharing!

    My web site; Fire damage restoration cost - Irwin,

  • Josefa
    回复

    Please let me know if you're looking for a writer for your blog.

    You have some really great articles and I think I would be
    a good asset. If you ever want to take some of the load off, I'd absolutely love to write some
    articles for your blog in exchange for a link back to mine.
    Please send me an e-mail if interested. Kudos!

    my web-site - SaaS Tax recruitment firm

  • Filomena
    回复

    When some one searches for his necessary thing, therefore he/she
    needs to be available that in detail, therefore that thing is maintained over here.

    Also visit my website: Topics for evaluation essay

  • Ezekiel
    回复

    Hey I know this is off topic but I was wondering if you knew of any widgets
    I could add to my blog that automatically tweet my newest twitter
    updates. I've been looking for a plug-in like this for quite some time and
    was hoping maybe you would have some experience with something like this.

    Please let me know if you run into anything. I truly enjoy reading
    your blog and I look forward to your new updates.

    my web blog - Custom 18650 battery pack

  • Yasmin
    回复

    It's a pity you don't have a donate button! I'd certainly donate
    to this excellent blog! I suppose for now i'll settle for bookmarking and adding your RSS
    feed to my Google account. I look forward to fresh updates and will talk about this website with my
    Facebook group. Chat soon!

    Feel free to surf to my blog post; Best free plagiarism checker reddit

  • Lon
    回复

    Valuable information. Fortunate me I found your website unintentionally, and
    I am shocked why this coincidence didn't took place in advance!
    I bookmarked it.

    My homepage; 레플리카 (marketingcompanies82692.jts-blog.com)

  • Delphia
    回复

    Spot on with this write-up, I honestly believe this site needs
    far more attention. I'll probably be back again to read more, thanks for the info!

    Feel free to surf to my page :: 반찬쇼케이스

  • Aleisha
    回复

    Thanks for your personal marvelous posting! I quite enjoyed reading it, you will be a great
    author. I will make certain to bookmark your blog and
    may come back someday. I want to encourage you to continue your great job, have a nice
    morning!

    Look at my web-site - Epsom Salt

  • Thao
    回复

    First off I would like to say terrific blog! I had a quick question which I'd like to
    ask if you don't mind. I was curious to find out how you center
    yourself and clear your mind before writing. I've had a tough time clearing my thoughts in getting my thoughts out there.
    I truly do enjoy writing however it just seems like the first 10 to 15 minutes
    are generally wasted simply just trying to figure out how to begin. Any recommendations or tips?

    Kudos!

    Review my page T-shirt med eget tryck

  • Sherri
    回复

    Hey there I am so thrilled I found your site, I
    really found you by mistake, while I was researching on Digg for something else, Nonetheless I am here
    now and would just like to say thank you for a incredible post and a all round enjoyable blog
    (I also love the theme/design), I don’t have time to browse it all at the moment but I have saved it and also added your RSS feeds, so when I have time I
    will be back to read a lot more, Please do keep
    up the awesome work.

    My web blog - Shop apple earpods online (https://marketingcompaniesnearme26160.blogaritma.com/3638712/exactly-what-are-the-most-effective-headphones)

  • Izetta
    回复

    A person essentially assist to make significantly posts I
    might state. This is the very first time I frequented your website page and so far?
    I amazed with the analysis you made to make this
    actual publish incredible. Fantastic job!

    Feel free to visit my site :: Startimes customer care number

  • Brock
    回复

    This is the perfect web site for anybody who hopes to find out about this topic.
    You understand a whole lot its almost tough to argue with you (not that I really will need
    to…HaHa). You certainly put a brand new spin on a subject that has been written about for ages.
    Excellent stuff, just great!

    Have a look at my blog - Eating Out On Keto - Eduardo -

  • Miquel
    回复

    Howdy! This blog post could not be written any better!

    Reading through this article reminds me of my previous roommate!
    He continually kept preaching about this. I will forward this
    information to him. Fairly certain he'll have a good read.
    Thank you for sharing!

    Take a look at my site Full cow qurbani

  • Ulysses
    回复

    Thanks for finally talking about > WordPress优化之去掉分类链接中的category - wordpress中文企业主题|模板|教程|插件下载安装-wordpress汉化主题开发 < Loved it!

    Here is my web blog: แทงบอล

  • Rebecca
    回复

    Very nice post. I just stumbled upon your weblog and wished to say that I have
    really enjoyed browsing your blog posts. In any case I will be subscribing to your
    feed and I hope you write again soon!

    Here is my homepage; Party Wall Agreement (Kara)

  • Roseann
    回复

    Your style is so unique compared to other people I have read stuff from.
    Thanks for posting when you have the opportunity, Guess I will just book mark this blog.

    My web page: Legal Process Server

  • Tammi
    回复

    Good day! I could have sworn I've been to this blog before
    but after browsing through some of the post I realized it's new to me.
    Nonetheless, I'm definitely happy I found it and I'll be
    book-marking and checking back frequently!

    my webpage; Bass Tackle Lures (marketing-firm61727.gynoblog.com)

  • Clay
    回复

    It's a pity you don't have a donate button! I'd certainly donate to this fantastic blog!

    I suppose for now i'll settle for book-marking and adding your RSS feed
    to my Google account. I look forward to brand new updates and will talk about this website with my
    Facebook group. Chat soon!

    Look at my blog post; buy lebanese hash online

  • Russell
    回复

    I'm extremely pleased to discover this page. I wanted to thank you for ones
    time just for this fantastic read!! I definitely
    liked every bit of it and i also have you book-marked to see new things
    on your site.

    Look into my web-site Can cbd oil make you sleepy

  • Victorina
    回复

    Hey there! I know this is kind of off topic but I was wondering which blog platform are you using for
    this website? I'm getting tired of Wordpress
    because I've had issues with hackers and I'm looking
    at options for another platform. I would be fantastic
    if you could point me in the direction of a good platform.

    my web blog :: Tennis court construction cost

  • Chris
    回复

    Its such as you learn my mind! You appear to know a lot about this,
    such as you wrote the book in it or something. I believe that you simply can do with some p.c.
    to power the message house a little bit, however other than that, this is great blog.
    A great read. I'll definitely be back.

    My web blog; Best essay writing service reviews

  • 相关文章