Registering Custom Post Types


Hello!

I've just discovered Beans and finding it so cool and having some fun but also frustrations - due to my own lack of knowledge. I've searched the Support and Community pages but I can't figure out how to register a custom post type. I want to be able to set up a CPT so that a user can select within WP admin and create an item, add an image and some meta data that will be output to a specific page grid. I can't even work out the Custom post type bit so am probably a little in under my head, but if anyone can assist, I'd be grateful.

TA!


The way you create a custom post type doesn't change when using Beans. It's the same as with any other theme. The easiest way to register a custom post type is with a plugin like Custom Post Type UI, which lets you do it without any coding. Otherwise if you're comfortable with PHP (or even not so comfortable, but at least familiar) you can do it yourself by adding some code to your theme's functions.php file, using the register_post_type() function which you can read about on the Codex at the Post Types page or the Function Reference.

Here's a simple example:

function my_post_type() {
    register_post_type( 'my_post_type',
    array(,
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'author'),
    )
    );
}

add_action('init', 'my_post_type');

The array of options for the custom post type can be deeply customized according to what you want to do with it, and all the available options are listed in the Function Reference page I linked to earlier.

Then to display your custom post ype, you'll need a custom loop, which is a bit more complicated. Depending on where you're planning to display it, you may want to create a custom page template and inside of that replace the default Beans loop with your custom loop. Doing this from scratch would be hard, so to make things easier you can copy the contents of Beans' loop.php file (located in lib > templates > structure) into your new template file, and using that as a starting point, modify it to display your custom post type by replacing the default while loop with a custom one. This could end up looking something like the below code:

<?php

do_action( 'beans_before_loop' );

if ( have_posts() && ! is_404() ) :

    do_action( 'beans_before_posts_loop' );

    beans_modify_action_callback( 'beans_loop_template', 'my_custom_loop' );

else :

    do_action( 'beans_no_post' );

endif;

        function my_custom_loop() {
            // Set your query.
            $my_query = new WP_Query( 'post_type=my_post_type' );
            // Loop
            while ( $my_query->have_posts() ) : $my_query->the_post();

                $article_attributes = array(
                    'id'        => get_the_ID(), // Automatically escaped.
                    'class'     => implode( ' ', get_post_class( array( 'uk-article', ( current_theme_supports( 'beans-default-styling' ) ? 'uk-panel-box' : null ) ) ) ), // Automatically escaped.
                    'itemscope' => 'itemscope',
                    'itemtype'  => 'http://schema.org/CreativeWork',
                );

                    beans_open_markup_e( 'beans_post', 'article', $article_attributes );

                        beans_open_markup_e( 'beans_post_header', 'header' );

                            do_action( 'beans_post_header' );

                        beans_close_markup_e( 'beans_post_header', 'header' );

                        beans_open_markup_e( 'beans_post_body', 'div', array( 'itemprop' => 'articleBody' ) );

                            do_action( 'beans_post_body' );

                        beans_close_markup_e( 'beans_post_body', 'div' );

                    beans_close_markup_e( 'beans_post', 'article' );

                endwhile;

                do_action( 'beans_after_posts_loop' );

                do_action( 'beans_after_loop' );

                    wp_reset_postdata();
        }

beans_load_document();

If you're not sure what to name your template file, check this page from the WordPress Developer documentation. In the example above I wanted to modify the frontpage, so I titled the file home.php. There are many other options depending on your usage case. And also I should note that the example above is meant to create an archive for a custom post type; if you want to make a template for a single page, that would be different, though in many cases the default behavior will be fine.


Oh wow, thanks so much, that's great! I think I've been over thinking it, this is awesome, I'll play around with that, cheers Balaram.

Write a reply

Login or register to write a reply, it's free!