自定义永久链接仅具有分类法和邮政名称,而没有CPT

时间:2019-07-18 12:02:37

标签: php wordpress permalinks

我需要将永久链接结构更改为以下内容: https://example.com/taxonomie/postname

当前是这样的:

https://example.com/customPostType/taxonomie/postName

我尝试使用以下代码:

function remove_cpt_slug( $post_link, $post ) {
if ( 'customPosttype' === $post->post_type && 'publish' === $post->post_status ) {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );

function add_cpt_post_names_to_main_query( $query ) {
    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }
    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
        return;
    }
    // Bail 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', 'holzbau' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );

可以正常工作,即按我的要求正确构建了URL,但仍然出现404错误。

您对我有什么解决办法吗?谢谢!

0 个答案:

没有答案