• Resolved Mike_sa

    (@mike_sa)


    Hi

    Is there a way with super cpt to add a meta box to a specific page template?

    I have tried a few thing including using the is_page_template()

    I appreciate any help pointing me in the right direction.

    This is what I have

    add_action('after_setup_theme','trusted_page_sup');
    function trusted_page_sup(){

    $tsuppliers2 = new Super_Custom_Post_Meta( 'page' );

    $tsuppliers2->add_meta_boxes(array(
    'id' => 'Contact-details',
    'context' => 'side',
    'priority'=> 'high',
    'post-type' => 'page',
    'fields' => array(

    'base' => array('type'=>'select', 'options' => array('vermont','cape town')),
    'Address' => array( 'type' => 'textarea' ),
    'Tel:' => array('type' => 'text'),
    'Fax' => array('type' => 'text'),
    'Email' => array('type' => 'text'),
    'Website' => array('type' => 'text'),
    )
    ));

    }

    http://wordpress.org/plugins/super-cpt/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Matthew Boynes

    (@mboynes)

    Hey Mike,
    Interesting question. There are two ways you can do this: you can either use javascript to show/hide the meta box based on the value of the page template, or you can only show it if the appropriate page template is selected. The former is probably the better way to go, because you could show/hide the meta box immediately as the page template dropdown changes. The latter will only show/hide after the post is saved. I was intrigued by that route all the same, so I threw some code together for it that I’m pretty sure will work:

    add_action( 'after_setup_theme','trusted_page_sup' );
    function trusted_page_sup() {
    	if ( isset( $_GET['post'] ) )
    		$post_id = $_GET['post'];
    	elseif ( isset( $_POST['post_ID'] ) )
    		$post_id = $_POST['post_ID'];
    	else
    		$post_id = get_the_ID();
    
    	if ( ! $post_id )
    		return;
    
    	if ( 'page' != get_post_type( $post_id ) )
    		return;
    
    	if ( 'page-template.php' == get_post_meta( $post_id, '_wp_page_template', true ) ) {
    		$tsuppliers2 = new Super_Custom_Post_Meta( 'page' );
    		$tsuppliers2->add_meta_boxes( array(
    			'id' => 'Contact-details',
    			'context' => 'side',
    			'priority'=> 'high',
    			'post-type' => 'page',
    			'fields' => array(
    				'base' => array('type'=>'select', 'options' => array('vermont','cape town')),
    				'Address' => array( 'type' => 'textarea' ),
    				'Tel:' => array('type' => 'text'),
    				'Fax' => array('type' => 'text'),
    				'Email' => array('type' => 'text'),
    				'Website' => array('type' => 'text'),
    			)
    		) );
    	}
    }

    Good luck, and let me know how you make out!

    Cheers,
    Matt

    Thread Starter Mike_sa

    (@mike_sa)

    Awesome 🙂 Thanks so much 🙂
    It works perfectly 🙂 I must investigate the javascript route 🙂

    I have one last question for you well it could be 2 😀

    I am wanting to populate the select menu with the terms of a taxonomy….
    Could you please point me in the right direction ..

    I am not sure if I should be using the $tsuppliers2->add_to_columns………
    or how to go about it…

    I was planning on using the get_terms($taxonomy_name) yet it returns nothing on the back end yet of the public side of the site it return an array no problem at all ….

    Sorry a bit off track there 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Adding meta box to admin side of specific page template’ is closed to new replies.