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.