WordPress Plugins and Themes

breadcrumb trail for custom post types

3 Replies ·

  1. I have permalinks set as /blog/%postname%/

    All works well except when any single-custom_post_type.php file is loaded.

    Home /Business Listings /Listing Category is a trail in which the second and third are pages with shortcodes to display the content using page.php which is fine.

    ‘Listing Category’ is a child page of ‘Business Listings’

    ‘Listings’ is also a child page of ‘Business Listings’,

    however the shortcode on the page ‘Listings’ loads single-custom_post_type.php and the breadcrumb becomes
    ‘Home /Blog /Listings /Single listing name’

    The behavior is the same for any single custom post.

    Could you suggest a fix?

  2. Justin Tadlock

    The “Blog” part of that often happens when a CPT has the with_front option set to true.

    Can you check args for your register_post_type()? It should look something like this:

    'rewrite' => array(
        'slug'       => 'slug',
        'with_front' => false,
        'pages'      => true,
        'feeds'      => true,
        'ep_mask'    => EP_PERMALINK,
    ),
    

    The with_front bit should be false.

  3. markmcl

    I have

    function railtrail_custom_post_type_args( $args, $post_type ) {
        if ( $post_type == "acadp_listings" ) {
            $args['rewrite'] = array(
                'with_front' => false,
                'pages'      => true,
                'feeds'      => true,
                'ep_mask'    => EP_PERMALINK,
                );
    }
    
        return $args;
    }
    add_filter( 'register_post_type_args', 'railtrail_custom_post_type_args', 20, 2 );
    
    
    function railtrail2_custom_post_type_args( $args, $post_type ) {
        if ( $post_type == "event" ) {
            $args['rewrite'] = array(
                'with_front' => false,
                'pages'      => true,
                'feeds'      => true,
                'ep_mask'    => EP_PERMALINK,
                );
    }
    
        return $args;
    }
    add_filter( 'register_post_type_args', 'railtrail2_custom_post_type_args', 20, 2 );
    
    

    which works.

    Is this the best way to do and could you help combine into one statement?

  4. Justin Tadlock

    I’m thinking you can simplify it down to just this:

    add_filter( 'register_post_type_args', function( $args, $post_type ) {
    
        $types = [
            'acadp_listings',
            'events'
        ];
    
        if ( in_array( $post_type, $types ) ) {
            $args['rewrite']['with_front'] = false;
        }
    
        return $args;
    
    }, 20, 2 );