Question

I want to be show a metabox on the specific page templates and hide on the others. Here is the code what I am using. This is works with one page. But what is the right syntax to add more page? Ex: template-2.php and template-3.php? Thanks.

jQuery(document).ready( function() {                       
        jQuery('#page_template').live('change', function(){
                if(jQuery(this).val() == 'template-1.php') {                    
                jQuery('#gallery-metabox').show();
            } else {         
                jQuery('#gallery-metabox').hide(); 
            }
        });                 
    }); 
Était-ce utile?

La solution

Here is the code, add your template file names in TempArray,

jQuery(document).ready(function() {
    var TempArray = [ "template-1.php","template-2.php", "template-3.php","template-4.php" ];
    jQuery('#gallery-metabox').hide();
    jQuery('#page_template').on('change', function(){
      if (jQuery.inArray($(this).val(), TempArray) !== -1) {
        jQuery('#gallery-metabox').show();
       } else jQuery('#gallery-metabox').hide();
     });                 
 }); 

DEMO

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top