permalinks - .HTML POST extension for custom post type - DRAFT - PREVIEW

admin2025-01-07  5

I use this code to "convert" custom post type in /customtype/postname.html

function rewrite_rules( $rules ) {
    $new_rules = array();
    foreach ( get_post_types() as $t )
        $new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=' . $t . '&name=$matches[1]';
    return $new_rules + $rules;
}
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
    global $post;
    $type = get_post_type( $post->ID );
    return home_url( $type . '/' . $post->post_name . '.html' );
}

It works so well.

But !

If you create a DRAFT post and after put cursor on PREVIEW (in wp - admin)

You will see url like this : /.html?preview=true

How I can fix it ? That problem only with "PREVIEW" link.

I use this code to "convert" custom post type in /customtype/postname.html

function rewrite_rules( $rules ) {
    $new_rules = array();
    foreach ( get_post_types() as $t )
        $new_rules[ $t . '/([^/]+)\.html$' ] = 'index.php?post_type=' . $t . '&name=$matches[1]';
    return $new_rules + $rules;
}
add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
    global $post;
    $type = get_post_type( $post->ID );
    return home_url( $type . '/' . $post->post_name . '.html' );
}

It works so well.

But !

If you create a DRAFT post and after put cursor on PREVIEW (in wp - admin)

You will see url like this : http://www.domain.com/customtype/.html?preview=true

How I can fix it ? That problem only with "PREVIEW" link.

Share Improve this question asked Dec 28, 2024 at 9:06 webstackoverloadwebstackoverload 1417 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Here is a simple solution, if post have DRAFT status - we show a "normal" do not rewrited "URL".

add_filter( 'post_type_link', 'custom_post_permalink' ); // for cpt post_type_link (rather than post_link)
function custom_post_permalink ( $post_link ) {
    global $post;
    $type = get_post_type( $post->ID );
    $post_status = get_post_status( $post->ID ); //get POST STATUS
    if($post_status != "draft") {
        return home_url('PUT YOUR CUSTOM POST TYPE HERE/'.$post->post_name.'.html');
    } else {
        //if POST STATUS is DRAFT
        return home_url('?post_type=PUT YOUR CUSTOM POST TYPE HERE&p='.$post->ID.'&preview=true');
    }

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255558a291.html

最新回复(0)