WordPress.org

Ready to get started?Download WordPress

Forums

Problem Adding Second Custom Post Type to Functions.php (5 posts)

  1. ben1000
    Member
    Posted 2 years ago #

    Hi there

    I am trying to add my second custom post type to Functions.php and have copied exactly the same code as works for the first one, changing the relevant words to relate to the new one, and pasted it below. When I try to access the dashboard after uploading, I continually receive this error message:

    Fatal error: Cannot redeclare codex_custom_init() (previously declared in /home/content/98/4487498/html/wp-content/themes/arthemia/functions.php:6) in /home/content/98/4487498/html/wp-content/themes/arthemia/functions.php on line 124

    Any ideas?
    Thanks

  2. Jacob Chappell
    Member
    Posted 2 years ago #

    Looks to me like you're re-using a function name (which you can't do). Every function must be unique and so you need to use some prefixes or suffixes to avoid collisions. For example, instead of using codex_custom_init() for your second post type, use something like codex_custom_init_two()

  3. Troy Chaplin
    Member
    Posted 2 years ago #

    Alternatively, you can have multiple custom post type within the same function like this:

    <?php
    
    function codex_custom_init() {
    
    // FIRST POST TYPE
    	register_post_type('post-type-one', array(
    		// add info here
    	) );
    
    // SECOND POST TYPE
    	register_post_type('post-type-two', array(
    		// add info here
    	) );
    
    // IMPORTANT: Remember this line!
    flush_rewrite_rules( false );
    }
    
    // Initialise custom post type
    add_action('init', 'codex_custom_init', 1);
    
    ?>
  4. ben1000
    Member
    Posted 2 years ago #

    Thanks!

  5. Zukes
    Member
    Posted 2 years ago #

    Thank you guys, really helpfull

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.