Bị lỗi plugin remove slug from custom post type năm 2024

You can rename it in the settings. But is it possible to remove it at all? To make it work domain.com/page_name/

Viewing 2 replies - 1 through 2 (of 2 total)

Viewing 2 replies - 1 through 2 (of 2 total)

I am trying to remove slug from custom post type called Articles. I was exploring StackOverflow and I found lots of discussions but they are not working. If you use rewrite with slug="/" it messes up other posts and pages. So is WordPress capable of removing slug or not? Thanks.

I already tried different solutions from the stack overflow.

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

marc_s

740k177 gold badges1348 silver badges1468 bronze badges

asked Oct 10, 2019 at 0:24

1

You could try this:

fw-portfolio is post-type slug name you should include yours

 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' );

answered Oct 10, 2019 at 5:35

Bị lỗi plugin remove slug from custom post type năm 2024

1

You could try this:

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 );
    }
});

Bị lỗi plugin remove slug from custom post type năm 2024

Suraj Rao

29.5k11 gold badges94 silver badges103 bronze badges

answered Jan 22, 2021 at 14:12

Bị lỗi plugin remove slug from custom post type năm 2024

try this chinesepost is slug name of custom post type

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' );
?>

Bị lỗi plugin remove slug from custom post type năm 2024

Pikamander2

7,7224 gold badges53 silver badges70 bronze badges

answered Oct 26, 2020 at 7:11

Bị lỗi plugin remove slug from custom post type năm 2024

Maulik patelMaulik patel

2,5862 gold badges20 silver badges26 bronze badges

3