Archived topic
Adding Custom Post Type
3 replies · Started by Pete on October 6, 2021
We are moving from an old parent/child theme from another vendor to GeneratePress (using only the parent theme since our site is relatively straight forward).
With our old theme we simply appended the following to the child functions.pbp ...
// Add custom post type -- stock_photo
function create_posttype_stock_photo() {
register_post_type( 'stock_photo',
// CPT Options
array(
'labels' => array(
'name' => __( 'Stock Photo' ),
'singular_name' => __( 'Stock Photo' )
),
'supports' => array('title', 'editor', 'custom-fields', 'excerpt', 'thumbnail', 'comments'),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-chart-line',
'rewrite' => array('slug' => 'stock_photo'),
'show_in_rest' => true
)
);
}
add_action( 'init', 'create_posttype_stock_photo' );
This rested in the following post structure -- https://oursite.com/stock_photo/sample
For our new GeneratePress Pro theme we referred to https://docs.generatepress.com/article/adding-php/ and added the Code Snippets plugin and made a snippet with the above PHP snippet but any "stock_photo" posts can't be found.
What are we missing?
Hi there,
that code simply registers the CPT. You need to create some templates for it to be displayed.
This article provides a simple method to do that:
https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/
Please note you will need a child theme:
Thank you for the quick response.
A few clarifications...
-- Forgot to mention that we had previously installed the Marketer template from the Site Library. Does this impact adding a custom post type?
-- From https://docs.generatepress.com/article/using-child-theme/ it is mentioned... if all you'll be doing is adding CSS or PHP, a child theme probably isn't necessary. So can we avoid going with a child theme if all we need to do is add some plugins, CSS and a few PHP snippets?
-- Added a template by copying single.php and the content-single.php and then renaming to single-stock_photo.php and content-stock_photo.php in the /generatepress/ directory per https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/. Also, changed the line in question to get_template_part( 'content', 'stock_photo' );
Unfortunately, that didn't get the custom post type working.
Did we implement everything properly?
If so, what are we missing?
A few clarifications…
— Forgot to mention that we had previously installed the Marketer template from the Site Library. Does this impact adding a custom post type?
Not really. CPTs are posts as well so it'll just copy the default template for posts unless you go set a template for it.
— From https://docs.generatepress.com/article/using-child-theme/ it is mentioned… if all you’ll be doing is adding CSS or PHP, a child theme probably isn’t necessary. So can we avoid going with a child theme if all we need to do is add some plugins, CSS and a few PHP snippets?
That's right. But you seem to be need custom templates so you may need it.
— Added a template by copying single.php and the content-single.php and then renaming to single-stock_photo.php and content-stock_photo.php in the /generatepress/ directory per https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/. Also, changed the line in question to get_template_part( ‘content’, ‘stock_photo’ );
you should put it on the directory of the child theme. :)