Archived topic
Creating Custom Post Types via code
5 replies · Started by Sophie on August 16, 2020
Hi there! I'm new to this theme and using it alongside Elementor Pro.
I want to add custom post types for portfolios, and have read that while a plugin may be easier this isn't always ideal in case there are issues with the plugin.
Therefore I've added the following code (though this was via the Code Snippets plugin, so not sure if that could be an issue too), and would like to know a) if everything looks correct (it seems to work ok), b) if theme or WP updates will affect this code? c) if I should enter the code directly into a file myself?
I just want to make sure there are no issues with it down the track, and any entries entered into these custom types won't be lost. Thanks so much! (also the site is under construction so not live)
Here's the code:
/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'Portfolio',
// CPT Options
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'work'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
/* Custom Post Type End */
part two:
/* Custom Post Type Start */
function create_posttype() {
register_post_type( 'Portfolio',
// CPT Options
array(
'labels' => array(
'name' => __( 'Portfolio' ),
'singular_name' => __( 'Portfolio' )
),
'public' => true,
'has_archive' => false,
'rewrite' => array('slug' => 'work'),
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
/* Custom Post Type End */
Hi there,
looks ok to me.
Theme and WP updates won't affect that code whilst it is saved in your Code Snippets.
Hi David, thanks for getting back to me so quickly! Would I be better adding this into the functions.php file instead?
In your Child Themes functions.php would be the best place for it.
Don't add it to the parent theme functions.php
Got it! Thanks for your help :)
Awesome :)