Create a Custom Post Type

Welcome mkmcst community Forum Web programming WordPress Random wordpress Create a Custom Post Type

Viewing 0 reply threads
  • Author
    Posts
    • #8863
      Michael Mc
      Keymaster
      @mkmcst

      Custom post types help you organize different content types, such as testimonials or products, and enhance user experience. You might want to consider a plugin if you’re building a complex site with multiple post types. However, if your needs are simpler, such as adding just one or two post types, a code snippet can efficiently do the job.

      This code snippet lets you create a new custom post type called ‘Product’, which you can rename as needed. To use it, create a new PHP file in your theme’s main folder, like ‘product.php’, and insert the code snippet there. Then, using include() or require(), include this file in your theme’s ‘functions.php’ file.

      Or, you can directly paste the code into ‘functions.php’. The code works by using the ‘init’ action hook to register the post type after WordPress loads but before the page starts loading.

      function codewp_create_post_type() {
        register_post_type( 'Product',
          array(
            'labels' => array(
              'name' => __( 'Products' ),
              'singular_name' => __( 'Product' )
            ),
            'public' => true,
            'has_archive' => true,
          )
        );
      }
      add_action( 'init', 'codewp_create_post_type' );
Viewing 0 reply threads
  • You must be logged in to reply to this topic.