How do you add a custom post type in your Beaver Builder theme without any plugin? I want to create a custom post type “portfolio” to showcase my work. I used the following PHP scripts in Beaver Builder Child theme.
Register Custom Post Type “Portfolio”
Open the class-fl-child-theme.php file from bb-theme-child/classes folder. Add the following PHP snippet above the last curly bracket ‘}‘.
static public function register_custom_post_type_portfolio() { $portfolio_labels = array( 'name' => __( 'Portfolio', 'taxonomy general name', 'fl-builder'), 'singular_name' => __( 'Portfolio Item', 'fl-builder'), 'search_items' => __( 'Search Portfolio Items', 'fl-builder'), 'all_items' => __( 'Portfolio', 'fl-builder'), 'parent_item' => __( 'Parent Portfolio Item', 'fl-builder'), 'edit_item' => __( 'Edit Portfolio Item', 'fl-builder'), 'update_item' => __( 'Update Portfolio Item', 'fl-builder'), 'add_new_item' => __( 'Add New Portfolio Item', 'fl-builder') ); $portfolio_slug = 'portfolio-item'; $args = array( 'labels' => $portfolio_labels, 'rewrite' => array('slug' => $portfolio_slug,'with_front' => false), 'singular_label' => __('Portfolio', 'fl-builder'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'hierarchical' => false, 'menu_position' => 9, 'menu_icon' => 'dashicons-art', 'supports' => array( 'title', 'editor', 'thumbnail' ) ); register_post_type( 'portfolio' , $args ); }
Now open the functions.php file of BB child theme and add this single line at end of the file:
add_action( 'init', 'FLChildTheme::register_custom_post_type_portfolio' );
After this you can do extra things. You can register the custom taxonomy for portfolio category. I used the following the steps.
Register Taxonomy “portfolio-cat”
Again open the class-fl-child-theme.php file and add this small PHP snippet there.
static public function register_taxonomy_portfolio_cat() { $category_labels = array( 'name' => __( 'Categories', 'fl-builder' ), 'singular_name' => __( 'Category', 'fl-builder' ), 'search_items' => __( 'Search Portfolio Categories', 'fl-builder' ), 'all_items' => __( 'All Portfolio Categories', 'fl-builder' ), 'parent_item' => __( 'Parent Portfolio Category', 'fl-builder' ), 'edit_item' => __( 'Edit Portfolio Category', 'fl-builder' ), 'update_item' => __( 'Update Portfolio Category', 'fl-builder' ), 'add_new_item' => __( 'Add New Portfolio Category', 'fl-builder' ), 'menu_name' => __( 'Categories', 'fl-builder' ) ); $permalink_slug = 'portfolio-cat'; register_taxonomy("portfolio-cat", array("portfolio"), array( 'hierarchical' => true, 'labels' => $category_labels, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => $permalink_slug ) ) ); }
Later you will add this single PHP code into the functions.php file.
add_action( 'init', 'FLChildTheme::register_taxonomy_portfolio_cat' );
Here is the complete code of class-fl-child-theme.php file.
/** * Helper class for child theme functions. * * @class FLChildTheme */ final class FLChildTheme { /** * Enqueues scripts and styles. * * @return void */ static public function enqueue_scripts() { wp_enqueue_style( 'fl-child-theme', FL_CHILD_THEME_URL . '/style.css' ); } /** * Register custom post type. New CPT: portfolio * * @return void */ static public function register_custom_post_type_portfolio() { $portfolio_labels = array( 'name' => __( 'Portfolio', 'taxonomy general name', 'fl-builder'), 'singular_name' => __( 'Portfolio Item', 'fl-builder'), 'search_items' => __( 'Search Portfolio Items', 'fl-builder'), 'all_items' => __( 'Portfolio', 'fl-builder'), 'parent_item' => __( 'Parent Portfolio Item', 'fl-builder'), 'edit_item' => __( 'Edit Portfolio Item', 'fl-builder'), 'update_item' => __( 'Update Portfolio Item', 'fl-builder'), 'add_new_item' => __( 'Add New Portfolio Item', 'fl-builder') ); $portfolio_slug = 'portfolio-item'; $args = array( 'labels' => $portfolio_labels, 'rewrite' => array('slug' => $portfolio_slug,'with_front' => false), 'singular_label' => __('Portfolio', 'fl-builder'), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'hierarchical' => false, 'menu_position' => 9, 'menu_icon' => 'dashicons-art', 'supports' => array( 'title', 'editor', 'thumbnail' ) ); register_post_type( 'portfolio' , $args ); } /** * Register custom taxonomy for portfolio post type. * * @return void */ static public function register_taxonomy_portfolio_cat() { $category_labels = array( 'name' => __( 'Categories', 'fl-builder' ), 'singular_name' => __( 'Category', 'fl-builder' ), 'search_items' => __( 'Search Portfolio Categories', 'fl-builder' ), 'all_items' => __( 'All Portfolio Categories', 'fl-builder' ), 'parent_item' => __( 'Parent Portfolio Category', 'fl-builder' ), 'edit_item' => __( 'Edit Portfolio Category', 'fl-builder' ), 'update_item' => __( 'Update Portfolio Category', 'fl-builder' ), 'add_new_item' => __( 'Add New Portfolio Category', 'fl-builder' ), 'menu_name' => __( 'Categories', 'fl-builder' ) ); $permalink_slug = 'portfolio-cat'; register_taxonomy("portfolio-cat", array("portfolio"), array( 'hierarchical' => true, 'labels' => $category_labels, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => $permalink_slug ) ) ); } }
Reset the Permalink Settings
We shall avoid the 404 error for single portfolio and taxonomy archive pages. Navigate to Dashboard -> Settings -> Permalink page and click on “Save Changes” button. It will flush the permalink structure and new permalinks of Portfolio items will work perfectly.