skip to Main Content

can someone tell my, why this code isn’t working for projects custom post type on Divi? I would really appreciate some help here. I tried to do it many ways, but it doesn’t work.

// delete slug from projects custom post type
function na_remove_slug( $post_link, $post, $leavename ) {

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

function na_parse_request( $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', 'project', 'page' ) );
    }
}
add_action( 'pre_get_posts', 'na_parse_request' );

2

Answers


  1. Chosen as BEST ANSWER

    thx for you help I did what I wanted with this type of function

    function custom_post_type_projects() {
        $args = array(
            'public' => true,
            'label'  => 'Projects',
            'rewrite' => array(
                'slug' => 'pisanie-prac',
                'with_front' => false
            ),
        );
        register_post_type('project', $args);
    }
    add_action('init', 'custom_post_type_projects');
    

    there is no /blog/ and I added my subpage in structure of URLs, it works perfectly also with YOAST SEO breadcrumbs, showing the exact structure I wanted.


  2. It could be because:

    • rewrite rules may not be updated correctly
    • If any other plugin or theme feature might conflict with your rewrite rules or custom post types, deactivate them temporarily to ensure no conflicts.
    • Ensure that your permalink settings are set to "Post Name" in the WordPress setting

    Here’s your adjusted code with the potential improvements:

    // Remove slug from 'project' post type URLs
    function na_remove_slug( $post_link, $post, $leavename ) {
    
        if ( 'project' != $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', 10, 3 );
    
    function na_parse_request( $query ) {
    
        if ( ! $query->is_main_query() || ! isset( $query->query['name'] ) ) {
            return;
        }
    
        // Debugging line
        error_log( 'Project post type detected!' );
    
        if ( ! empty( $query->query['name'] ) ) {
            $query->set( 'post_type', array( 'post', 'project', 'page' ) );
        }
    }
    add_action( 'pre_get_posts', 'na_parse_request' );
    
    // Temporarily flush rewrite rules (remove after testing)
    function na_flush_rewrite_rules() {
        flush_rewrite_rules();
    }
    add_action( 'after_switch_theme', 'na_flush_rewrite_rules' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search