Support » Fixing WordPress » Problem Adding Second Custom Post Type to Functions.php

  • 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

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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()

    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);
    
    ?>

    Thanks!

    Thank you guys, really helpfull

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Problem Adding Second Custom Post Type to Functions.php’ is closed to new replies.