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 trial

WordPress

Felix Dorner
Felix Dorner
791 Points

Coding custom post types and fields in the functions.php

Hi,

is there any possibility to code the custom post types and fields into the functions.php. I know that there might be disadvantages, but when creating a premium theme, potential clients demand that everything works right out of the box without any or less plugins.

Can someone give me some hints or resources where to learn such approach? I'd like to know how to code a theme options page as well... Information on that topic are also welcome!

Thanks in advance, Felix

2 Answers

Matt Campbell
Matt Campbell
9,767 Points

Hi Felix,

Yes, it is in fact where you code the custom post type. Same for meta boxes. However, I tend to, where I can, pull the files into the functions.php page when possible. Not always so as can cause conflicts.

Anyway, to create a custom post type, enter the following.

add_action('init','reviews');
function reviews(){
$labels = array(
    'name' => 'Reviews', 
    'singular_name' => 'Review', 
    'add_new' => 'Add New Review',
    'add_new_item' => 'Post Review',  
    );

$args = array(
    'labels' => $labels,
    'public' => true,
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions'),
    'has_archive' => true,
    'taxonomies' => array('category', 'reviews'),
    'menu_position' => 5,
    'rewrite' => true,
    );

register_post_type('reviews', $args);

}

I've just pulled that from a functions.php file for a theme I'm working on at the moment. You can get all the relevant arguments from the codex.

Kevin Korte
Kevin Korte
28,149 Points

I've used plugins because they just work, but can understand your reasoning.

It is possible to code it into the functions.php file.

http://codex.wordpress.org/Post_Types http://codex.wordpress.org/Custom_Fields