Skip to content

Instantly share code, notes, and snippets.

@juyal-ahmed
Last active August 29, 2015 14:07
Show Gist options
  • Save juyal-ahmed/54b55d84ff60bfaeedd7 to your computer and use it in GitHub Desktop.
Save juyal-ahmed/54b55d84ff60bfaeedd7 to your computer and use it in GitHub Desktop.
Loading Show/Hide different settings meta block for Page, Post, Post Type (i.e : Portfolio) when page template changed on WordPress admin by a Javascript which is loaded for admin
/*
---------------------------------------------------------------------------------------
Loading different settings meta block for Page, Post, Portfolio when page template changed on WordPress admin
---------------------------------------------------------------------------------------
*/
jQuery(document).ready( function($) {
/*
---------------------------------------------------------------------------------------
To make below post_type condition working you need to use below codes in php when you load this code contained file for WordPress admin.
wp_enqueue_script('wp-admin-page-template-selection-js', get_template_directory_uri() . '/resources/js/custom-admin.js', array('jquery'), '1.2', true );
// Localize the script with template uri.
$post_type = 'post';
if ( !empty( $_REQUEST['post'] ) ) {
$post_type = get_post_type($_REQUEST['post']);
} else if ( !empty( $_REQUEST['post_type'] ) ) {
$post_type = $_REQUEST['post_type'];
}
$translation_array = array( 'template_directory_uri' => get_template_directory_uri(), 'post_type' => $post_type );
wp_localize_script( 'wp-admin-page-template-selection-js', 'theme', $translation_array );
---------------------------------------------------------------------------------------
*/
if ( theme.post_type == 'slider' ) {
$('#postimagediv').hide();
}
var $page_template = $('#page_template');
$page_template.change(function() {
if ($(this).val() == 'templates/template-portfolio1.php') {
$('#portfolio1').show();
$('#portfolio2').hide();
$('#portfolio3').hide();
$('#portfolio4').hide();
$('#page-special').hide();
} else if ($(this).val() == 'templates/template-portfolio2.php') {
$('#portfolio2').show();
$('#portfolio1').hide();
$('#portfolio3').hide();
$('#portfolio4').hide();
$('#page-special').hide();
} else if ($(this).val() == 'templates/template-portfolio3.php') {
$('#portfolio3').show();
$('#portfolio1').hide();
$('#portfolio2').hide();
$('#portfolio4').hide();
$('#page-special').hide();
} else if ($(this).val() == 'templates/template-portfolio4.php') {
$('#portfolio4').show();
$('#portfolio1').hide();
$('#portfolio2').hide();
$('#portfolio3').hide();
$('#page-special').hide();
} else if ($(this).val() == 'default' || $(this).val() == 'templates/template-archives.php' || $(this).val() == 'templates/template-page-half-width.php' || $(this).val() == 'templates/template-page-full-width.php' || $(this).val() == 'templates/template-contact.php') {
$('#portfolio4').hide();
$('#portfolio1').hide();
$('#portfolio2').hide();
$('#portfolio3').hide();
$('#page-special').show();
} else {
$('#portfolio1').hide();
$('#portfolio2').hide();
$('#portfolio3').hide();
$('#portfolio4').hide();
$('#page-special').hide();
}
}).change();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment