Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKonrad Pilch
2,435 PointsIs there anything wrong with this?
//Celebrities
// Our custom post type function
function create_posttype() {
register_post_type( 'celebrity',
// CPT Options
array(
'labels' => array(
'name' => __( 'celebrity' ),
'singular_name' => __( 'celebrity piece' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'celebrity'),
'menu_icon' => 'dashicons-universal-access', // picture on the left [pic]Portfolio
)
);
}
flush_rewrite_rules();
//Music
// Our custom post type function
function create_posttype_music() {
register_post_type( 'music',
// CPT Options
array(
'labels' => array(
'name' => __( 'music' ),
'singular_name' => __( 'music' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'music'),
'menu_icon' => 'dashicons-admin-media', // picture on the left [pic]Portfolio
)
);
}
flush_rewrite_rules();
function create_posttype_professions() {
register_post_type( 'professions',
// CPT Options
array(
'labels' => array(
'name' => __( 'Professions' ),
'singular_name' => __( 'profession' )
),
'public' => true,
'has_archive' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
add_action( 'init', 'create_posttype_music' );
add_action( 'init', 'create_posttype_profession' );
1 Answer
Sue Dough
35,800 PointsOH MY YES! Do not use this just yet.
flush_rewrite_rules(); will rock your world and bring your site to a halt like this. Why would you call it multiple times? You do realize that it isn't on the init function and so it will run on every page load? I would strongly reccomend not using flush_rewrite_rules unless you have to.
Also some of your code formatting is not good:
bad spacing:
'rewrite' => array('slug' => 'music'),
good spacing:
'rewrite' => array( 'slug' => 'music' ),
bad indentation:
'rewrite' => array('slug' => 'music'),
'menu_icon' => 'dashicons-admin-media', // picture on the left [pic]Portfolio
good indentation:
'rewrite' => array( 'slug' => 'music' ),
'menu_icon' => 'dashicons-admin-media', // picture on the left [pic]Portfolio
The code formatting is based on WordPress coding standards. I would suggest to look it up and get familiar with it early on. It will help you a bunch in the long run.