I’d like to have a custom field interface show up only when a certain template is assigned to a particular WordPress page.
Any ideas?
I’d like to have a custom field interface show up only when a certain template is assigned to a particular WordPress page.
Any ideas?
You must be logged in to post a comment.
The best way to approach this situation is via JavaScript. That way, whenever the selected value changes, you can instantly hide/show the related metabox.
Use
wp_enqueue_script()
infunctions.php
to load a custom JavaScript file in the admin area:The script itself, which requires jQuery, simply hides or shows a metabox based on the selected value in the page template dropdown list. In this example, I’m only showing the metabox for post thumbnails in case the default page template is selected:
And, just for fun, here’s a shorter, less verbose, version of the same script:
Rather than rendering the metabox only to hide it with jQuery you can use this. The only difference being that the hiding/showing of the metabox requires selecting ‘Update’ after changing the drop-down field.
Just update lines 6 and 7 as desired.