[Resolved] Creating Custom Post Types via code

Home Forums Support Creating Custom Post Types via code

  • This topic has 5 replies, 2 voices, and was last updated 1 month ago by David.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1405512
    Sophie

    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 */

    #1405925
    David
    Staff
    Customer Support

    Hi there,

    looks ok to me.
    Theme and WP updates won’t affect that code whilst it is saved in your Code Snippets.

    #1406964
    Sophie

    Hi David, thanks for getting back to me so quickly! Would I be better adding this into the functions.php file instead?

    #1407345
    David
    Staff
    Customer Support

    In your Child Themes functions.php would be the best place for it.
    Don’t add it to the parent theme functions.php

    #1408676
    Sophie

    Got it! Thanks for your help 🙂

    #1409009
    David
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.