• Resolved abmiller99

    (@abmiller99)


    Hello, I love this plugin for theme development.

    My issue: I can’t get the ‘show_on’ filter to function with an array of page templates.

    Example:

    
    'object_types'  => array( 'post', 'portfolio', 'page' ),
    'show_on'       => array(
        'key' => 'page-template',
        'value' => array(
            'page.php',
            'single.php',
            'single-portfolio.php',
            'page-portfolio.php',
            'home.php',
            'page-html_header.php'
            )
         ),
    

    When I use that example, the metabox is does not appear on any of the templates listed.

    If I delete the ‘show_on’ filter, the metabox appears correctly, but on every template..

    Thanks for your help.

    Andy

    • This topic was modified 7 years, 9 months ago by abmiller99. Reason: formatting
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    Generally speaking, it’s better to use the 'show_on_cb' param, as then you can simply use a callback to determine if your box should show. In that callback, you would check if the post object has a particular template. Also, you’re template names look like they may not be page templates, but standard theme templates. Do these files have the template header in them?

    To use the 'show_on_cb' You would do something like:

    
    $cmb_demo = new_cmb2_box( array(
    	'id'            => 'yourprefix_demo_metabox',
    	'title'         => esc_html__( 'Test Metabox', 'cmb2' ),
    	'object_types'  => array( 'page' ), // Post type
    	'show_on_cb' => 'yourprefix_show_if_on_specific_page_templates', // function should return a bool value
    ) );
    

    And the yourprefix_show_if_on_specific_page_templates() function:

    
    function yourprefix_show_if_on_specific_page_templates( $cmb ) {
    	$slug = get_page_template_slug( $cmb->object_id );
    
    	return in_array( $slug, array( 'page.php', 'single.php', 'single-portfolio.php', 'page-portfolio.php', 'home.php', 'page-html_header.php' ) );
    }
    
    Thread Starter abmiller99

    (@abmiller99)

    OK thanks Justin, I was able to make that work based on your recommendations. And also I adjusted my page template filenames as they were not correct according to WP standards.

    Cheers

    Plugin Author Justin Sternberg

    (@jtsternberg)

    ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using ‘show_on’ with an array of page templates, can’t get it to work…’ is closed to new replies.