1

Looking for some information as developer docs for the cherry framework seem to be non existent.

I am trying to add a custom post-type with the following code in my theme-init file: (this code works fine in a standard installation used with a theme with the cherry framework it does not display i just get a 404 error)

    <?php

add_action( 'after_setup_theme', 'my_setup' );

if ( ! function_exists( 'my_setup' ) ):

function my_setup() {

    // This theme styles the visual editor with editor-style.css to match the theme style.
    add_editor_style();

    // This theme uses post thumbnails
    if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
        add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 299, 190, true ); // Normal post thumbnails
        add_image_size( 'slider-post-thumbnail', 1386, 563, true ); // Slider Thumbnail
        add_image_size( 'slider-thumb', 96, 41, true ); // Slider Small Thumbnail
    }

    // Add default posts and comments RSS feed links to head
    add_theme_support( 'automatic-feed-links' );

    // custom menu support
    add_theme_support( 'menus' );
    if ( function_exists( 'register_nav_menus' ) ) {
        register_nav_menus(
            array(
              'top_header_menu' => 'Top Header Menu',
              'header_menu' => 'Header Menu',
              'footer_menu' => 'Footer Menu'
            )
        );
    }
}
endif;


/* Slider */
function my_post_type_slider() {
    register_post_type( 'slider',
                array( 
                'label' => __('Slides', CURRENT_THEME), 
                'singular_label' => __('Slide', CURRENT_THEME),
                '_builtin' => false,
                'exclude_from_search' => true, // Exclude from Search Results
                'capability_type' => 'page',
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => false,
                'rewrite' => array(
                    'slug' => 'slide-view',
                    'with_front' => FALSE,
                ),
                'query_var' => "slide", // This goes to the WP_Query schema
                'menu_icon' => get_template_directory_uri() . '/includes/images/icon_slides.png',
                'supports' => array(
                        'title',
                        'custom-fields',
            'thumbnail')
                    ) 
                );
}

add_action('init', 'my_post_type_slider');



/* Portfolio */
function my_post_type_portfolio() {
    register_post_type( 'portfolio',
                array( 
                'label' => __('Projects', CURRENT_THEME),
                'singular_label' => __('Project', CURRENT_THEME),
                '_builtin' => false,
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => true,
                'hierarchical' => true,
                'capability_type' => 'page',
                'menu_icon' => get_template_directory_uri() . '/includes/images/icon_portfolio.png',
                'rewrite' => array(
                    'slug' => 'portfolio-view',
                    'with_front' => FALSE,
                ),
                'supports' => array(
                        'title',
                        'editor',
                        'thumbnail',
                        'excerpt',
                        'custom-fields',
                        'comments')
                    ) 
                );
    register_taxonomy('portfolio_category', 'portfolio', array('hierarchical' => true, 'label' => 'Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
    register_taxonomy('portfolio_tag', 'portfolio', array('hierarchical' => false, 'label' => 'Tags', 'singular_name' => 'Tag', 'rewrite' => true, 'query_var' => true));
}

add_action('init', 'my_post_type_portfolio');


/* Testimonial */
function my_post_type_testi() {
    register_post_type( 'testi',
                array( 
                'label' => __('Testimonial', CURRENT_THEME), 
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => false,
                'menu_position' => 5,
                'rewrite' => array(
                    'slug' => 'testimonial-view',
                    'with_front' => FALSE,
                ),
                'supports' => array(
                        'title',
                        'custom-fields',
                        'thumbnail',
                        'editor')
                    ) 
                );
}

add_action('init', 'my_post_type_testi');


/* Services */
function my_post_type_services() {
    register_post_type( 'services',
                array( 
                'label' => __('Services', CURRENT_THEME), 
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => false,
                'menu_position' => 5,
                'rewrite' => array(
                    'slug' => 'services-view',
                    'with_front' => FALSE,
                ),
                'supports' => array(
                        'title',
                        'thumbnail',
                        'editor')
                    ) 
                );
}

add_action('init', 'my_post_type_services');



/* FAQs */
function phi_post_type_faq() {
    register_post_type('faq', 
                array(
                'label' => __('FAQs', CURRENT_THEME),
                'singular_label' => __('FAQ', CURRENT_THEME),
                'public' => false,
                'show_ui' => true,
                '_builtin' => false, // It's a custom post type, not built in
                '_edit_link' => 'post.php?post=%d',
                'capability_type' => 'post',
                'hierarchical' => false,
                'rewrite' => array("slug" => "faq"), // Permalinks
                'query_var' => "faq", // This goes to the WP_Query schema
                'supports' => array('title','author','editor'),
                'menu_position' => 5,
                'publicly_queryable' => true,
                'exclude_from_search' => false,
                ));
}
add_action('init', 'phi_post_type_faq');


/* Our Team */
function my_post_type_team() {
    register_post_type( 'team',
                array( 
                'label' => __('Our Team', CURRENT_THEME), 
                'singular_label' => __('Person', CURRENT_THEME),
                '_builtin' => false,
                'exclude_from_search' => true, // Exclude from Search Results
                'capability_type' => 'page',
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => false,
                'menu_position' => 5,
                'rewrite' => array(
                    'slug' => 'team-view',
                    'with_front' => FALSE,
                ),
                'supports' => array(
                        'title',
                        'custom-fields',
                        'editor',
            'thumbnail')
                    ) 
                );
}

add_action('init', 'my_post_type_team');

function products_post_type() {

    $labels = array(
        'name'                => _x( 'Products', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Product', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Products', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Product:', 'text_domain' ),
        'all_items'           => __( 'All Products', 'text_domain' ),
        'view_item'           => __( 'View Product', 'text_domain' ),
        'add_new_item'        => __( 'Add New Product', 'text_domain' ),
        'add_new'             => __( 'New Product', 'text_domain' ),
        'edit_item'           => __( 'Edit Product', 'text_domain' ),
        'update_item'         => __( 'Update Product', 'text_domain' ),
        'search_items'        => __( 'Search products', 'text_domain' ),
        'not_found'           => __( 'No products found', 'text_domain' ),
        'not_found_in_trash'  => __( 'No products found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'product', 'text_domain' ),
        'description'         => __( 'Product information pages', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
        'taxonomies'          => array( 'category', 'post_tag' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'product', $args );

}



// Hook into the 'init' action
add_action( 'init', 'products_post_type');



function my_connection_types() {
    p2p_register_connection_type( 
        array(
            'name' => 'testi_to_product',
            'from' => 'testi',
            'to' => 'product',
            'admin_box' => 
            array(
                'show' => 'any',
                'context' => 'advanced'
            )
        )
    );
}
add_action( 'p2p_init', 'my_connection_types' );
?>

after creating new products i am unable to get the post to display, i just get a 404 error, for the category and the single page.

has anyone managed to add a new post-type to the cherry framework and can point me in the right direction for integrating it properly?

  • Have you added products_post_type() to a hook? Also, don't forget to flush (re-save) your permalinks afterwards. – helgatheviking Aug 21 '14 at 8:39
  • thnaks for your comment, yes i have refreshed the slugs, not sure where i need to add a hook, when i did this in a vanilla wordpress theme, i didnt need to add a hook, it just worked. – Dizzy Bryan High Aug 21 '14 at 9:38
  • ahh yes i do have a hook, '// Hook into the 'init' action add_action( 'init', 'products_post_type');' Updated my code, i had forgotten that line in my sample! – Dizzy Bryan High Aug 21 '14 at 9:46
1

At the end of your code after the last } add :

add_action('init,'products_post_type');

Make sure you're adding this to the file in the /wp-content/your_custom_theme/includes/theme-init.php file, and then of course update the permalink.

With the CherryFramework, make sure you don't modify any of the files in the CherryFramework folder, or you'll lose out when an update happens.

I just tested your code on a Cherry dev site I have, and it worked fine. There must be more to your theme-init?

  • i'll post the complete init file i have in the original post. – Dizzy Bryan High Aug 21 '14 at 10:01
  • Why testi->product? – Howard E Aug 21 '14 at 10:04
  • its a plugin called posts to posts, it basically allows me to link testimonials to a product, so once i can get a product to actually display i will add the code to loop through the testimonials linked to that product. – Dizzy Bryan High Aug 21 '14 at 10:08
  • ive commented out that bit of code, in case it was conflicting but i still get the 404 error when viewing the product – Dizzy Bryan High Aug 21 '14 at 10:11
  • Have you gone to admin>settings>permalinks and then click "Save Changes" ? – Howard E Aug 21 '14 at 10:12
1

Try adding:

add_action( 'init', 'products_post_type' );

As you can see in the Codex example for register_post_type() you must add your function to the init hook, so that it is actually run. Otherwise, what you have, is a function that is defined, but never executed.

If you did this with a default theme, then you were adding the function to an action hook. I don't know what a theme-init.php file is in the Cherry framework, but post types really don't belong in a theme anyway. What happens when you need to change themes?

I would suggest moving your post type code to a plugin or even a site-specific snippets plugin

  • thanks for that, its a child theme of cherry-framework, and is where the theme is initiated, i updated my original post with the hook, which was there, i just missed the line when i copied and pasted. the admin works fine it lists all the posts, edit delete save and publish seem to work ok, i just cant view the post in the page instead i get a 404 error – Dizzy Bryan High Aug 21 '14 at 9:50
  • 1
    You need to refresh (ie: re-save) your permalinks. Still, I would suggest moving all CPT definitions to a plugin. Themes should only deal with appearance. – helgatheviking Aug 21 '14 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.