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

qhhrdooz  于 2023-11-17  发布在  WordPress
关注(0)|答案(3)|浏览(159)

我试图从自定义文章类型中删除slug。我正在探索StackOverflow,我发现了很多讨论,但它们不起作用。如果您使用slug="/”重写,它会弄乱其他文章和页面。所以WordPress是否能够删除slug?谢谢。
我已经尝试了不同的解决方案从堆栈溢出。

'rewrite' => array( 
        'slug'          => '/',
        'with_front'    => false 
    ),

字符串

exdqitrt

exdqitrt1#

你可以试试这个:
fw-portfolio是post类型slug名称,您应该包括您的名称

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

    function gp_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', 'fw-portfolio' ) );
    }
    add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

字符串

yyhrrdl8

yyhrrdl82#

你可以试试这个:

add_filter( 'post_type_link', function( $post_link, $post, $leavename ) {

    $post_types = array(
        'product_pages',
        'my_test_page'
    );

    if ( in_array( $post->post_type, $post_types ) && 'publish' === $post->post_status ) {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }

    return $post_link;

}, 10, 3 );

add_action( 'pre_get_posts', function( $query ) {

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

    
    if ( ! empty( $query->query['name'] ) ) {

            $post_types = array(
            'post', 
            'page', 
            'product_pages',
            'my_test_page'
        );

        $query->set( 'post_type', $post_types );

    }

});

字符串

ehxuflar

ehxuflar3#

试试这个中文帖子是自定义帖子类型的slug名称

<?php
function na_remove_slug_chinesespost( $post_link, $post, $leavename ) {

    if ( 'chinesespost' != $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', 'na_remove_slug_chinesespost', 10, 3 );

function na_parse_request_chinesespost( $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', 'chinesespost', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request_chinesespost' );
?>

字符串

相关问题