Archived topic
Creating a custom post type and archive
8 replies · Started by Filip on February 23, 2022
Hi!
I'm creating a Case study custom post type for my website and I'm lost.
I tried following your tutorial but it's still not working.
I've added this code to my child theme's functions.php:
function create_post_type() {
register_post_type('case-study',
array(
'labels' => array(
'name' => __('Case studies', 'textdomain'),
'singular_name' => __('Case study', 'textdomain'),
),
'public' => true,
'has_archive' => true,
)
);
}
add_action('init', 'create_post_type');
I've also downloaded a copy of single.php and content-single.php, renamed them to single-case-study.php and content-case-study.php and uploaded them in my child theme's folder.
I've replaced the code in a single-case-study.php (line 29) with the one you provided:
get_template_part( 'content', 'project' );
The thing is - I can add the case studies in the WP dashboard, but I can't view them and see the archives (404 errors).
Please help, thanks!
Hi there,
You need to replace the projectwith your custom post type name in get_template_part( 'content', 'project' );
In your case, case-study.
Oh, I've replaced that too, just as you said (accidentally pasted the default line in the original post).
Can you try change permalinks setting to post name to test?
The setting can be found at dashboard > settings > permalinks
It works now.
Thanks so much!
Everything's perfect but there's still one more issue.
I've applied the same template for a blog post and a case study - but the width of the content for the case study CPT is not applied.
Here's the image.
1st img - post
2nd img - case study
3rd img - content settings applied to both
Hi there,
change:
get_template_part( 'content', 'case-study' );
to:
generate_do_template_part( 'case-study' );
Works fine now, thanks once more!
Glad to hear that!