Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从wordpress url中删除自定义帖子类型?_Wordpress_.htaccess_Url_Custom Post Type - Fatal编程技术网

如何从wordpress url中删除自定义帖子类型?

如何从wordpress url中删除自定义帖子类型?,wordpress,.htaccess,url,custom-post-type,Wordpress,.htaccess,Url,Custom Post Type,我有一个wordpress网站,它使用自定义模板和自定义帖子类型,如登陆和服务 每个帖子类型在url中都有一个特定的slug,如下=>() 我想将此url()更改为此url() 事实上,我需要从url中删除[landing]短语。重要的是,[landing]是我的posts表中的自定义post类型 我已经测试了以下解决方案: ==>我已将register_post_type()中的rewrite属性中的slug更改为“/”-->它会中断所有登录、发布和页面url(404) ==>我在重写属性--

我有一个wordpress网站,它使用自定义模板和自定义帖子类型,如登陆和服务

每个帖子类型在url中都有一个特定的slug,如下=>()

我想将此url()更改为此url()

事实上,我需要从url中删除[landing]短语。重要的是,[landing]是我的posts表中的自定义post类型

我已经测试了以下解决方案:

==>我已将register_post_type()中的rewrite属性中的slug更改为“/”-->它会中断所有登录、发布和页面url(404)

==>我在重写属性-->中添加了“with_front”=>false,但没有任何更改

==>我尝试在htaccess-->中使用RewriteRule执行此操作,但它不起作用,或者出现了太多重定向错误

我没有得到一个合适的结果


以前有人解决过这个问题吗?

首先,您需要过滤自定义帖子类型的永久链接,以便所有已发布的帖子的URL中都不包含slug:

function stackoverflow_remove_cpt_slug( $post_link, $post ) {
    if ( 'landing' === $post->post_type && 'publish' === $post->post_status ) {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'stackoverflow_remove_cpt_slug', 10, 2 );
此时,尝试查看链接将导致404(未找到页面)错误。这是因为WordPress只知道帖子和页面可以有像
domain.com/post name/
domain.com/page name/
这样的URL。我们需要告诉它,我们的自定义帖子类型的帖子也可以有类似于
domain.com/cpt post name/
的URL

function stackoverflow_add_cpt_post_names_to_main_query( $query ) {
    // Return if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }
    // Return if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    // Return if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'landing' ) );
}
add_action( 'pre_get_posts', 'stackoverflow_add_cpt_post_names_to_main_query' );

嗨,企业家们,谢谢你们的回答。你能告诉我在哪里使用这些代码吗?在worpress core中还是在我的模板中?在哪个文件中?我不知道如何使用这些代码。你能帮我吗@创业教育-您只需将它们粘贴到
functions.php
文件中即可。
function{..}
code定义了我们想要使用的新函数,然后
add_filter
add_action
命令告诉WordPress何时使用它们。