1

What I Have Set Up

I have three custom post types, they are named "brands", "careers", and "news".
I have three page templates, they are named "page-brands", "page-careers", and "page-news".
The pages I created using those templates run a query for the custom posts with the same name (i.e. my "News" page lists all of the "news" custom posts).

The slugs for my custom post types are "brands", "careers", and "news".

The URL's for my pages are:

www.example.com/our-brands/
www.example.com/our-careers/
www.example.com/our-news/

I gave the pages those slugs because I know that the pages themselves cannot have the same slug as the custom post type, or WordPress will get confused and start giving 404 errors.

My Issue

The permalinks for my pages work, and the permalinks for my custom posts work, but I cannot get my breadcrumbs to get the right parent page URL.

For example: if you were to visit www.example.com/our-news/ and click on a "news" post that is listed there, the resulting URL would be www.example.com/news/example-news-custom-post/.
Because of this, my breadcrumbs shows up as "Home » News » Example News Custom Post". If someone wanted to use the breadcrumbs to navigate back, and clicked on the "News" link, they would get a 404 error because www.example.com/news/ does not exist.

What I Have Tried

I tried to remedy this by changing the rewrite argument (available when registering post types) so that the rewrite slug matches the url of the page I created.

'rewrite'             => array( 
                'slug'       => 'our-news',
                'with_front' => true,
                'pages'      => true,
                'feeds'      => true,
),

Of course, this did not work, because WordPress is now confused and my "News" page starts giving a 404 error.

The next thing I tried was to create a meta box to give my custom post types a parent page, using the ID of the pages I created to designate them as the parent of the custom post.

function rmm_add_meta_box() {

    $screens = array( 'brands', 'news' , 'careers');    

    foreach ( $screens as $screen ) {

        add_meta_box(
            'custom_post_parent',
            'Enter Post Parent ID',
            'rmm_add_meta_callback',
            $screen,
            'side'
        );
    }
}
add_action( 'add_meta_boxes' , 'rmm_add_meta_box' );

function rmm_add_meta_callback( $post ) {
    wp_nonce_field( basename( __FILE__ ), 'rmm_meta_nonce');

    $custom_post_id  = get_post_custom($post->ID);
    $parent_id       = $custom_post_id['parent_id'][0];
    $rmm_stored_meta = get_post_meta( $custom_post_id );

    ?>
    <input type="text" id="parent_id" name="parent_id" value="<?php if ( ! empty( $rmm_stored_meta['parent_id'] ) ) echo esc_attr( $rmm_stored_meta['parent_id'][0] ) ; ?>" />
    <?php
}

function rmm_meta_save( $post ){

    $is_autosave    = wp_is_post_autosave( $post_id );
    $is_revision    = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'rmm_meta_nonce' ] ) && wp_verify_nonce( $_POST[ 'rmm_meta_nonce' ], basename( __FILE__) ) ) ? 'true' : 'false';

    if ( $is_autosave || $is_revision || !$is_valid_nonce){

        return;
    }

    $data = sanitize_text_field( $_POST['parent_id'] );

    if ( isset( $_POST['parent_id'] ) ) {

        update_post_meta($post_id, 'parent_id', $data);
    }
}
add_action( 'save_post', 'rmm_meta_save');

This resulted in the "our-news" slug just being tacked onto the "news" slug. In other words: www.example.com/news/example-news-custom-post/ simply became www.example.com/our-news/news/example-news-custom-post/, with my breadcrumbs directing you to a parent page with the URL www.example.com/our-news/news/ (which, of course, does not exist).

I would like to know how I can get my parent page url to be www.example.com/our-news/ with my custom post url as www.example.com/our-news/example-news-custom-post so that my breadcrumbs will stop directing me to URL's of pages that don't exist.

  • How are breadcrumbs generated? Can you not modify those instead? – Milo Oct 11 '15 at 18:11
  • @Milo I'm using the Yoast SEO plugin. – Codette Oct 11 '15 at 18:17
  • 1
    There are filters to let you modify breadcrumbs if you look in the documentation for that plugin. Another option is to not use pages and instead use the post type archives that WordPress automatically generates. – Milo Oct 11 '15 at 18:41
  • @Milo I really don't want to get into modifying a plugin, because I can't be sure things will work properly once it updates. I also just tried creating an archive template and using it to populate the custom posts, instead of a page, and this does not remedy the problem. The permalink is still wrong. – Codette Oct 11 '15 at 19:21
  • I can't imagine the plugin author would break their own filters on update. I don't really see any other option for you. I would personally just write my own breadcrumb code. – Milo Oct 11 '15 at 20:20
0

Okay, I was overthinking this by a mile, because it didn't occur to me that WordPress automatically generates archive pages for custom post types if you set "has archive" to "true" when you're registering post types.

What Wound Up Working

I created customized archive-{slug}.php templates in my theme files for the "brands", "careers", and "news" custom post types that I had registered. Once that was done, I uploaded my theme and added the permalink (which WordPress automatically generates for each archive page) to my navigation menu via the custom link option. This resulted in a page with the correct url www.example.com/news/, posts with the correct url www.example.com/news/example-news-custom-post, and breadcrumbs that say "News" before the post title, with the correct URL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.