Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other WordPress 3.0 – Custom Post Types and Meta Boxes

  • This topic is empty.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #29694
    blue642
    Member

    Hello,

    I am posting this here to help a buddy from Twitter figure out a solution to a problem he ran into.

    So, the problem was needing to create user-editable additional content for a post like seen in the picture. http://twitpic.com/27fpu0

    The way I accomplished this was to create a custome post type ( I reused one from a test theme I was working on called "achievement"

    in the functions file…

    add_action('init', 'wlg_cstm_register');

    function wlg_cstm_register() {

    $achievement_labels = array(
    'name' => _x('Achievements', 'post type general name'),
    'singular_name' => _x('Achievement', 'post type singular name'),
    'add_new' => _x('Add New', 'achievement'),
    'add_new_item' => __('Add New Achievements'),
    'edit_item' => __('Edit Achievements'),
    'new_item' => __('New Achievement'),
    'view_item' => __('View Achievements'),
    'search_items' => __('Search Achievements'),
    'not_found' => __('No achievements found'),
    'not_found_in_trash' => __('No achievements found in Trash'),
    'parent_item_colon' => ''
    );

    $achievement_args = array(
    'labels' => $achievement_labels,
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => true,
    'supports' => array('title', 'editor', 'thumbnail')
    );


    register_post_type( 'achievement' , $achievement_args );
    }

    This code create a new post type called "achievement"

    I also added a custom meta box (this is basically like hard coding a custom field.)


    add_action("admin_init", "admin_init");
    add_action('save_post', 'save_points');

    function admin_init(){
    add_meta_box("achievementInfo-meta", "Achievement Points", "achievement_meta_options", "achievement", "side", "low");

    }

    function achievement_meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $points = $custom["points"][0];
    ?>

    }


    function save_points(){
    global $post;
    update_post_meta($post->ID, "points", $_POST["points"]);
    }

    It creates a textarea called "points" and adds this meta box only to the "achievement" post type. It then saves the value to a points meta option upon update.

    I then created a new file in my theme called single-achievement.php (this determines how the single achievement post will look.)




    $custom = get_post_custom($post->ID);
    $display_points = $custom["points"][0];
    ?>

    >





    Basically it just gets the content, the "points" value, and title.

    To show the posts in the main loop, I just did a query_posts and got both the "post" post type, and the custom "achievement" post type….

     array('post', 'achievement') ) );
    ?>


    id="post-">

    ">














    You can see it in action here, there is no styling on the theme…
    http://tests.dingledoodle.com/diet/

    I made the custom meta HTML lists to make sure HTML input works there.

    To add a second, just make a second meta box like above, and display that second meta info in the single-achievement.php template.

    #80239
    thatryan
    Member

    Thanks a ton man for writing this up, big help.
    I am not clear on the last piece of code though, to ‘show the posts in the main loop’ the one with the query,
    where does that get used? Does the code above it, in the single-achievement.php file not display the posts from that category?

    Thanks again!

    #80240
    Rob MacKay
    Participant

    Hey thanks for sharing this :) great place to post it, I am sure lot’s of people will find this useful :)

    #80243
    TheDoc
    Member

    *Move to Tips & Tricks

    This is awesome! I’ve been looking for a proper tutorial like this for a while, but one step further.

    What I want to do is have not a text field meta box thing, but a place for a user to select an image.

    #80265
    blue642
    Member

    Ryan,

    The "show the posts in the main loop" part is just saying that if you want the custom post type to show up in your main posts loop (say in the index.php, or home.php loop, you’ll have to create a custom query as is in last section of code.

    Thanks for moving it, and the compliments!

    TheDoc,

    I’ll look into trying that out in a bit, I think that could be cool…

    Though I believe if you weren’t using the post thumbnail you could just utilize the "featured image" and post_thumbnail to accomplish what you might want…

    http://codex.wordpress.org/Post_Thumbnails

    #93111
    geoff_joh
    Member

    This is awesome! It’s definitely one of my favorite tutorials for metaboxes!

    I have one question. what if I were going to create multiple metaboxes in a page and a post? is that much more difficult?

    #113497
    brnater
    Member

    Mil gracias, Chris! It took me having to go through about four tutorials to remember, “let me see if Chris has something on this.” Sure enough, you did, and it’s **much** easier to understand and replicate compared to what I’ve been staring at for the past 2 hours.

    You’re the man, as always.

    #113498
    Colorful Tones
    Participant

    This has been covered on WPTuts+ recently too: “[A Guide to WordPress Custom Post Types: Creation, Display and Meta Boxes](http://wp.tutsplus.com/tutorials/plugins/a-guide-to-wordpress-custom-post-types-creation-display-and-meta-boxes/ )

    #113522
    brnater
    Member

    @colorful_tones This was one of the posts that I saw that was more bulky and cumbersome than I needed.

    #129002
    nisarg7575
    Member

    Hey thanks for sharing this great place to post it, I am sure lot’s of people will find this useful

Viewing 10 posts - 1 through 10 (of 10 total)
  • The forum ‘Other’ is closed to new topics and replies.