WordPress.org

Ready to get started?Download WordPress

Forums

OptionTree
[resolved] Meta Boxs On Custom Post Types (8 posts)

  1. zackskeeter
    Member
    Posted 1 year ago #

    Hey, How can i add meta boxes to a custom post type? i have them on my pages but have no clue how to get it on a custom post type

    Here is my code

    $post_options = array(
        'id'        => 'page_options',
        'title'     => 'Page Options',
        'desc'      => '',
        'pages' 	=> 'portfolio',
        'context'   => 'normal',
        'priority'  => 'high',
        'fields'    => array(
    
         array(
        	'id' => 'title_1',
    		'label' => 'Page Specific Options',
    		'desc' => 'This is a set of custom options for just this page. These are entirely optional, but they bring some extra functionality to pages. Some page templates (Portfolios, Blogs, etc.) may need some of these options filled out to work properly.',
    		'type' => 'textblock',
    		'std' => '',
    		'class' => '',
          	'choices' => array()
    	),
    
          array(
        	'id' => 'post_count',
    		'label' => 'Number of Posts Per Page',
    		'desc' => 'If this is a page template that uses blog posts, set a number for how many posts you want to show up per page. The default is set to show ALL posts found within the above category(s).',
    		'type' => 'text',
    		'std' => '',
    		'class' => '',
          	'choices' => array()
    	),
       )
     );
      ot_register_meta_box( $post_options );

    http://wordpress.org/extend/plugins/option-tree/

  2. htvu
    Member
    Posted 1 year ago #

    In your options array use an Array for 'pages' value instead string (as your above code)

    'pages' => array('portfolio', 'your_cpt_slug'),

  3. zackskeeter
    Member
    Posted 1 year ago #

    Thanks :) that made it show up on in the right place. But i am getting this :

    Cannot modify header information - headers already sent by (output started at /home/zackusa/public_html/responsiveweb.co.za/wp-content/themes/grunge-portfolio/includes/new-meta-boxes.php:2) in /home/zackusa/public_html/responsiveweb.co.za/wp-includes/pluggable.php on line 876

    Using the following code

    <?php
    /**
     * Initialize the meta boxes before anything else.
     */
    add_action( 'admin_init', '_custom_meta_boxes' );
    
    /**
     * Builds the Meta Boxes.
     */
    function _custom_meta_boxes() {
    
      $meta_args_array = array(
        array(
          'id'          => 'page_settings',
          'title'       => 'Page Settings',
          'pages'       => array( 'page' ),
          'context'     => 'normal',
          'priority'    => 'high',
          'fields'      => array(
            array(
              'label'       => 'Portfolio Type',
              'id'          => 'portfolio_type',
              'type'        => 'radio',
              'desc'        => 'What type of portfolio item is this?',
              'choices'     => array(
                array(
                  'label'       => 'Standard',
                  'value'       => 'standard'
                ),
                array(
                  'label'       => 'Image',
                  'value'       => 'image'
                ),
                array(
                  'label'       => 'Gallery',
                  'value'       => 'gallery'
                ),
                array(
                  'label'       => 'Video',
                  'value'       => 'video'
                )
              ),
              'std'         => 'standard'
            )
          )
        ),
        array(
          'id'          => 'portfolio_settings',
          'title'       => 'Porftolio Settings',
          'pages' => array('portfolio', 'portfolio'),
          'context'     => 'normal',
          'priority'    => 'high',
          'fields'      => array(
            array(
              'label'       => 'Video Embed Code',
              'id'          => 'portfolio_video',
              'type'        => 'textarea-simple',
              'desc'        => 'Video Embed Code. The width and height should be: 950px * 385px',
              'std'         => '',
              'rows'        => '5'
            )
          )
        )
      );
    
      /* load each metabox */
      foreach( $meta_args_array as $meta_args ) {
        ot_register_meta_box( $meta_args );
      }
    }

    Any suggestions?

  4. htvu
    Member
    Posted 1 year ago #

    Check your new-meta-boxes.php file. Maybe it have some space before <?php

  5. zackskeeter
    Member
    Posted 1 year ago #

    Thanks, I was calling the files in the wrong order. Silly mistake. It all is working now and i have only one final question

    This may be really simple but i seem to be stuck on it.

    How do i use these meta box fields? When its in option tree i use the get_option_tree("theid");

    but is there anything i need to do for it to work here? for example if i had a field which gave the option of adding extra text to a place. What code would i use to echo it out?

    I have tried get_field() get_option() _custom_meta_boxes() and of course get_option_tree()

    Thanks so much for all your help thus far

  6. htvu
    Member
    Posted 1 year ago #

    Most of meta box's data is saved as post/page metadata so you just simple use: get_post_meta($your_post_id, 'your_field_id') :D

  7. zackskeeter
    Member
    Posted 1 year ago #

    Worked like a charm! Thank you!

  8. IntricatePixels
    Member
    Posted 1 year ago #

    Thanks saved me some time with this too. I was trying to output it the OT way, but they are metadata!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic