Is there a way to show custom page settings on specific page templates ONLY?
For instance I have a settings field: “Custom Settings”. I don’t want this to show on the “Default Page Template”. I only want it to display if we select the “Custom Page Settings Page Template”.
Here’s my meta box code:
function cd_meta_box_add()
{
add_meta_box( 'icon-class-meta-box', 'Icon Class', 'ic_meta_box_cb', 'page', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function ic_meta_box_cb( $post)
{
$values = get_post_meta( $post->ID );
$selected = isset( $values['ic_meta_box_select'] ) ? esc_attr( $values['ic_meta_box_select'][0] ) : â;
wp_nonce_field( 'ic_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="ic_meta_box_select">Class</label>
<select name="ic_meta_box_select" id="ic_meta_box_select">
<option value=" " <?php selected( $selected, ' ' ); ?>> </option>
<option value="red" <?php selected( $selected, 'red' ); ?>>red</option>
<option value="blue" <?php selected( $selected, 'blue' ); ?>>blue</option>
<option value="green" <?php selected( $selected, 'green' ); ?>>green</option>
<option value="pink" <?php selected( $selected, 'pink' ); ?>>pink</option>
<option value="orange" <?php selected( $selected, 'orange' ); ?>>orange</option>
<option value="black" <?php selected( $selected, 'black' ); ?>>black</option>
</select>
</p>
<?php
}
I typically use CSS and jQuery for this type of thing by hooking
admin_head
. This should be done client-site as a user has an option to select your template or another one after DOM load. Basically, all you do is check if the value is set for#page_template
and toggleshow/hide
if your template is selected or not.