TransWikia.com

I want to know if i can add two different custom post types to my wordpress site

WordPress Development Asked by Yogesh on October 30, 2021

I’m trying to add two custom post types, "articles" and "projects". Using a php code i found on wpbeginner, "articles" post has worked. But when I’m trying the same code with "projects" i’m getting a code error.

Your PHP code changes were rolled back due to an error on line 70 of
file wp-content/themes/generatepress-child/functions.php. Please fix
and try saving again.

Cannot redeclare create_posttype() (previously declared in
wp-content/themes/generatepress-child/functions.php:34)

    // Our custom post type function
function create_posttype() {
 
    register_post_type( 'articles',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Articles' ),
                'singular_name' => __( 'Article' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'articles'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );


// Our custom post type function
function create_posttype() {
 
    register_post_type( 'projects',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Projects' ),
                'singular_name' => __( 'Project' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'projects'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

One Answer

As Jacob Peattie pointed out in this comment, the reason why you code isn't working is because you have two functions with same name create_posttype. PHP assumes you want to re-declare the function, which you cannot do.

So in order for your code to work, you can either combine your custom post types creation into one function (as Jacob suggests):

    // Our custom post type function
function create_posttype() {
 
    register_post_type( 'articles',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Articles' ),
                'singular_name' => __( 'Article' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'articles'),
            'show_in_rest' => true,
 
        )
    );

    register_post_type( 'projects',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Projects' ),
                'singular_name' => __( 'Project' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'projects'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

... or give each function an unique name if you prefer to use two functions:

    // Our custom post type function
function create_posttype_articles() {
 
    register_post_type( 'articles',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Articles' ),
                'singular_name' => __( 'Article' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'articles'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_articles' );


// Our custom post type function
function create_posttype_projects() {
 
    register_post_type( 'projects',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Projects' ),
                'singular_name' => __( 'Project' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'projects'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_projects' );

Answered by Desert Fox on October 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP