Support » Plugin: CMB2 » Metabox in custom post type & page template

  • Resolved entumas

    (@entumas)


    Hi!

    I know it’s possible to show a metabox in a custom_post_type and at the same time in the pages.
    I also know that I can filter so that the metabox is only visible in the pages that use a particular page template.

    Is it possible to combine these 2 options?

    This means that this metabox is displayed in ‘my_custom_post_type’
    and also on the pages that use ‘my_page_template’.

    Something like this:

    'show_on' => array(
    	array( 'key' => 'post-type', 'value' => 'my-post-type' ),
    	array( 'key' => 'page-template', 'value' => 'my-page-template.php' ),
    	array( 'key' => 'page-template', 'value' => 'other-page-template.php' ),
    ),

    Gracias

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I would recommend looking over https://github.com/CMB2/CMB2/wiki/Adding-your-own-show_on-filters

    With a custom show_on_cb parameter, you’d want to return either true, to show it, or false to not show it.

    Note, the code has zero testing, and just want to demo the basic concepts.

    'show_on_cb' => 'maybe_show'

    function maybe_show($cmb2) {
        $type = get_post_type( $cmb2->object_id() );
        if ( 'my_custom_post_type' === $type ) {
            return true;
        }
    
        $template = get_post_meta( $cmb2->object_id(), 'page-template', true );
        if ( 'my-page-template.php' === $template ) {
            return true;
        }
    
        return false;
    }
    

    Evolve and mold as needed.

    Thank you Michael, I will review what you send me with attention 😉

    Hi Michael, sorry for not answering before!

    Your code works perfectly, it only has a small error, instead of ‘page-template’ is ‘_wp_page_template’.

    Thanks! 😀

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Whatever gets it working, right?

    Glad I could help

    Yes! 😀
    Thanks again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Metabox in custom post type & page template’ is closed to new replies.