重写网址以排除该段之后,如何删除旧的“自定义帖子类型"永久链接? [英] How do I delete the old Custom Post Type permalink after rewriting the URL to exclude the slug?

查看:66
本文介绍了重写网址以排除该段之后,如何删除旧的“自定义帖子类型"永久链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Book的Custom Post Type,链接为:mywebsite.com/book/mybookname

I have a Custom Post Type called Book, and the link is: mywebsite.com/book/mybookname

我想更改它,以便链接为mywebsite.com/mybookname.

I want to change this so that the link is mywebsite.com/mybookname.

我添加了以下代码来更改链接,它可以按预期工作:

I have added the following code to change the link and it works as expected:

function books_theme_remove_slug( $post_link, $post, $leavename ) {

    if ( 'book' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;
    }

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
}
add_filter( 'post_type_link', 'books_theme_remove_slug', 10, 3 );

function books_theme_parse_request( $query ) {

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'book', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );

问题是旧链接(mywebsite.com/book/mybookname)仍在工作.我想在不中断当前链接的情况下使该链接转到404页面.

The problem is that the old link(mywebsite.com/book/mybookname) is still working. I'd like to make that link go to a 404 page without breaking the current links.

我尝试了以下操作,但它破坏了所有内容:

I tried the following but it breaks everything:

function books_theme_parse_request( $query ) {
    if(isset($query->query['post_type']) && $query->query['post_type'] == 'book'){
        global $wp_query;
        $wp_query->set_404();
        status_header( 404 );
        get_template_part( 404 ); exit();
    }

    if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'book', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'books_theme_parse_request' );

如何删除旧网址?

推荐答案

这本来不应该发生的,所以您不应该尝试以编程方式对其进行修复-而是应从源头上对其进行修复.尝试找出原因并解决.否则,您可能会引入其他问题.

This shouldn't be happening in the first place, so you shouldn't try to fix it programmatically - instead you should fix it at source. Try to identify the cause and fix it. Otherwise you could introduce other issues down the line.

一些可能的解决方案,具体取决于原因:

Some possible solutions, depending on the cause:

  1. 刷新重写缓存
    WordPress不会将重定向写入.htaccess,而是使用 SarahCoding对删除旧的永久链接?"的答案

    操作方法:
    重新保存永久链接将刷新重写规则,但是如果这样做不起作用,则有在WordPress中刷新重写缓存的三种方法
     

  1. Flush your Rewrite Cache
    Wordpress doesn't write redirections into the .htaccess, it uses rewrite rules to parse the url and find a match for the redirection. It means that if you don't refresh your rewrite rules, the old links still work. Ref: SarahCoding's answer to 'Remove Old Permalinks?'

    How to do it:
    Re-saving your permalinks will flush the rewrite rules, but there if that doesn't work there are Three Ways to Flush the Rewrite Cache in WordPress
     

清除缓存
如果您安装了缓存插件,则需要清除它们.一些安全性插件还使用缓存,例如Securi.也可以将其缓存在浏览器中.

方法:
请参见

Clear your Cache
If you have an Caching plugins installed, they will need to be cleared. Some security plugins also use caching e.g. Securi. It could also just be cached in your browser.

How to do it:
See How to Clear Your Cache in WordPress
 

删除旧的WP永久链接
当您更新块时,旧的永久链接仍存储在数据库中.如果您想使用以前使用的弹头,则可能会导致问题.

操作方法:
旧的永久链接与_wp_old_slugmeta_key存储在表postmeta中.要清除所有旧块,请在WP数据库中运行以下查询:
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';

Ref
Mark Dave Tumanda对删除旧的永久链接?"的答案
 

Delete old WP permalinks
When you update a slug, the old permalinks are still stored in the database. This could cause issues if you want to use a slug that you had previously used for example.

How to do it:
The old permalinks are stored in the table postmeta with the meta_key of _wp_old_slug. To clear all of the old slugs, run this query in your WP database:
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';

Ref Mark Dave Tumanda's answer to 'Remove Old Permalinks?'
 

检查重定向插件
如果您使用的是任何重定向插件,请检查重定向规则,以防有任何与您的新网址冲突的地方.

Check Redirection Plugins
If you are using any redirection plugins, check the redirection rules in case there is anything there that is clashing with your new urls.

这篇关于重写网址以排除该段之后,如何删除旧的“自定义帖子类型"永久链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆