Okay
  Public Ticket #3555380
Add hero section to a new custom post
Closed

Comments

  • pandaman84 started the conversation

    hi, very cool theme. i use its child theme, and i created 2 new custom posts with some custom fields. I'd like to add the hero section (as on the pages, or portfolios), to have the same header on front page, with metaboxes on backend.

    There's a short method to add that functionality to my  child theme? 

    P.S. i registered the custom post with a plugin (following code)

    <?php
    /**
    Plugin Name: Jimbo
    **/
    /**
     * Register Custom Post Types
     *
     */
    function jimbo_register_product_cpt(){
        //Register Product Post Type
        register_post_type( 'product',
            array(
                'labels'  =--> array(
                    'name'           => __( 'Products', 'jimbo' ),
                    'singular_name'  => __( 'Products', 'jimbo' ),
                    'add_new'        => __( 'Add Product', 'jimbo' ),
                    'add_new_item'   => __( 'Add New Product', 'jimbo' ),
                    'edit_item'      => __( 'Edit Product', 'jimbo' ),
                    'all_items'      => __( 'All Products', 'jimbo' ),
                    'not_found'      => __( 'No Products Found', 'jimbo' ),
                ),
                'menu_icon'             => 'dashicons-layout',
                'public'                => true,
                'exclude_from_search'   => true,
                'has_archive'           => true,
                'hierarchical'          => false,
                'show_in_rest'          => true,
                'rewrite'               => array( 'slug' => 'products' ),
                'supports'              => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'excerpt', 'revisions', 'page-attributes', 'custom-fields' ),
                //'taxonomies'          => array( 'series')
            )
        );
    }
     add_action('init', 'jimbo_register_product_cpt');
     function jimbo_register_series_cpt(){
        //Register Series Post Type
        register_post_type( 'series',
            array(
                'labels'  => array(
                    'name'           => __( 'Series', 'jimbo' ),
                    'singular_name'  => __( 'Series', 'jimbo' ),
                    'add_new'        => __( 'Add Series', 'jimbo' ),
                    'add_new_item'   => __( 'Add New Series', 'jimbo' ),
                    'edit_item'      => __( 'Edit Series', 'jimbo' ),
                    'all_items'      => __( 'All Series', 'jimbo' ),
                    'not_found'      => __( 'No Series Found', 'jimbo' ),
                ),
                'menu_icon'             => 'dashicons-layout',
                'public'                => true,
                'exclude_from_search'   => true,
                'has_archive'           => true,
                'hierarchical'          => false,
                'show_in_rest'          => true,
                'rewrite'               => array( 'slug' => 'series' ),
                'supports'              => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'excerpt', 'revisions', 'page-attributes', 'custom-fields' ),
                //'taxonomies'          => array( 'series')
            )
        );
    }
     add_action('init', 'jimbo_register_series_cpt');
     function jimbo_register_custom_taxonomies(){
        // Add new taxonomy, make it hierarchical (like categories)
         $labels = array(
             'name'              => _x( 'Series', 'taxonomy general name', 'jimbo' ),
             'singular_name'     => _x( 'Series', 'taxonomy singular name', 'jimbo' ),
             'search_items'      => __( 'Search series', 'jimbo' ),
             'all_items'         => __( 'All Series', 'jimbo' ),
             'edit_item'         => __( 'Edit Series', 'jimbo' ),
             'update_item'       => __( 'Update Series', 'jimbo' ),
             'add_new_item'      => __( 'Add New Series', 'jimbo' ),
             'not_found'         => __( 'No Series Found!', 'jimbo' ),
         );
         $args = array(
             'hierarchical'      => true,
             'labels'            => $labels,
             'show_ui'           => true,
             'show_admin_column' => true,
             'show_in_rest'      => true,
             'has_archive'       => true,
             'rewrite'           => array('slug' => 'series-cat')
         );
         register_taxonomy( 'series-cat', array( 'product','series' ), $args ); 
     }
     add_action('init', 'jimbo_register_custom_taxonomies');
    /**
     * Register meta boxes.
     */
    function product_register_meta_boxes() {
        add_meta_box( 'product-meta', 'Jimbo', 'product_display_callback', 'product');
    }
    add_action( 'add_meta_boxes', 'product_register_meta_boxes' );
    /**
     * Meta box display callback.
     *
     * @param WP_Post $post Current post object.
     */
    function product_display_callback( $post ) {
        include plugin_dir_path( __FILE__ ) . './form.php';
    }
    function product_save_meta_box( $post_id ) {
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
        if ( $parent_id = wp_is_post_revision( $post_id ) ) {
            $post_id = $parent_id;
        }
        $fields = [
            'jimbo_code',
            'jimbo_data_sheet',
            'jimbo_instructions',
            'jimbo_2d',
            'jimbo_3d'
        ];
        foreach ( $fields as $field ) {
            if ( array_key_exists( $field, $_POST ) ) {
                update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
            }
         }
    }
    add_action( 'save_post_product', 'product_save_meta_box' );
    
  •  1,974
    Support replied

    Hello,

    plz use the example of the existing custom post type 'harington_portfolio'

    For custom post types to have their own page you need to link it with its name.

    So following our example for theme's portfolio it's

    single-harington_portfolio.php

    When you open it you will see the code that displays the hero section at the top:

    https://prnt.sc/XK-s5XtHu0eH

    Thank you.

    Clapat Support

    https://www.clapat.com/

    Review Envato Item Support Policy

  • pandaman84 replied

    You're right, i've already did it, but the code inside hero_section of serano theme is full of "if sections" looking for the portfolio custom post and for some theme options. So there's a lot of copy/paste... In fact i open this ticket looking for a shorter method.

    Ah i forget a thing... I need to add the hero metaboxes to my custom post, or that hero_section won't work... how can i do this?


  •  1,974
    Support replied

    Oh, that's right! You need to add also a new set of metaboxes for the custom post type.

    for this, plz open /include/metabox-config.php and duplicate the portfolio item options

    https://prnt.sc/fec5OzEqmVSf

    but you need to rename them

    for example let say the custom post type is 'serano_services'

    $portfolio_options = array();

    will become

    $services_options = array();

    and 

    serano-opt-portfolio-hero-img

    will become

    serano-opt-services-hero-img

    then you need to add a similar section in /include/hero-properties.php

    https://prnt.sc/bRqxjCKA6dmg


    Thank you.

    Clapat Support

    https://www.clapat.com/

    Review Envato Item Support Policy