Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ive searched around and havent been able to find anything regarding using a specific "template" as a target to trigger when to show custom meta boxes...Is this possible, and if so how would you script this out?

If post/page ID's are the only real feasible option then would it be possible to target a parent page ID rather than individual page IDs?

EDIT #1

To help further explain the particulars of project Im going to briefly outline why Id like to instantiate custom meta boxes for only particular page templates...

The way my site is currently setup is have a bunch of custom static pages for artists on a record label. Originally this was perfectly fine since they did not need and/or request a more dynamic solution - however now they are currently needing to frequently update their own bio/pics/etc for each artist profile page making my current setup not very useful.

Currently, there is a Recording Artists page that acts as an archive, and then all artist pages are children of that page.

Additionally, I also need to allow personalized widgets to each artists sidebar which is a key part of deciding which direction to go.

The only other alternative Ive considered but Im not sure if it would work is to convert the recording artist archive into a CPT archive instead, and then make each artist page a custom single page...

share|improve this question
    
Are you talking about admin area? If yes than you cann't separate pages when you create new one. But after first save you can filter as you wish, it may be page template or page id or parent page id. –  Alexey May 1 '12 at 4:46
    
Yes, in the admin (wordpress backend) area...What I basically need to do is only show specific custom meta boxes when a particular type of page is being created...not just "any" page. –  Mr.Brown May 1 '12 at 5:07
    
Describe what type of page do you mean please –  Alexey May 1 '12 at 6:26
    
Theres another good approach here: wordpress.stackexchange.com/questions/29286/… –  Dale Sattler May 1 '12 at 9:33
    
@Alexey - See my original post, Ive updated it with a more detailed explanation. –  Mr.Brown May 1 '12 at 15:10

2 Answers 2

up vote 5 down vote accepted

I needed the same thing, showing a metabox based on the selected page template, and since the user has to select a page template and save and only then i could know which metabox to show i ended up showing all and use some simple jQuery to only show the needed one without having to save first, here:

function custom_metabox_per_template() {
    global $pagenow,$typenow;
    if ( is_admin() && in_array( $pagenow, array( 'post.php', 'post-new.php' ) ) && $typenow == 'page') {
        $script = <<< EOF;
<script type='text/javascript'>
    jQuery(document).ready(function($) {

        //hide all metaboxs
        function hide_all_custom_metaboxes(){
            $('#full-with.php').hide();
            $('#showcase.php').hide();
            $('#no-sidebar-page.php').hide();
        }

        //show a metabox
        function show_custom_metabox(meta_id){
            var selector = "#"+meta_id;
            if( $(selector).length)
                $(selector).show();
        }

        //first hide all metaboxes
        hide_all_custom_metaboxes();

        //then check for selected page template and show the corect metabox
        var current_metabox = $('#page_template').val();
        show_custom_metabox(current_metabox);

        //and last listen for changes update when needed
        $('#page_template').bind("change", function(){
            hide_all_custom_metaboxes();
            show_custom_metabox($('#page_template').val());
        });
    });
</script>
EOF;
        echo $script;
    }
}
add_action('admin_footer', 'custom_metabox_per_template');

the trick is to give your metabox id that matches the name of your template file, you can see in this example: full-with.php , showcase.php , no-sidebar-page.php are the names of the theme files that define the page templates, and when i user changes the page template the shown metabox changes as well.

share|improve this answer
    
Yeah, the save part is the catch indeed, I thought of this as well...thats why Im starting to potentially think that targeting the parent page ID may be the best route for my situation...but this is definitely a method I would have not thought of with the JQuery. –  Mr.Brown May 1 '12 at 15:09
    
just like you can't determine the selected page template before the page is saved, the same gos for the page parent, you need to first save the page, so targeting the parent page is no so useful, or maybe you can tell me more and I'll understand better. –  Bainternet May 1 '12 at 16:24
    
Bummer, no your right...I didnt think you had to save first if you already had the ID of the parent but that makes sense in retrospect. –  Mr.Brown May 1 '12 at 19:31

I would solve it like this:

1) Advanced Custom Fields to generate custom meta boxes (containing custom fields managed through ACF) that will display selectively according to a set of rules ACF creating a new custom field

2) Widget Logic to show a Text Widget only in the pages you need

3) enable shortcodes in the Text Widget, and build one to suit your needs (display styled info according to some criteria)

4) if you need, you can make custom functions to use with Widget Logic, i.e. is_artist('name-of-artist) or is_record_label('name-of-label')

[UPDATE]

These questions (duplicates) can be handy too:

Custom meta box shown when template is chosen

Toggle admin metabox based upon chosen page template

share|improve this answer
    
I appreciate the in-depth response @brasofilo. I may have to use this solution if I cant make it work in the box. –  Mr.Brown May 1 '12 at 23:58
    
@Mr.Brown, it'd be nice to see the code where you construct the meta boxes, and also what's the exact behavior you are expecting... You say the artist info is being set in the profile page, do you want to do that in a regular page now? –  brasofilo May 2 '12 at 10:48
    
Apologies, but I have not created it yet since I need to first determine the best route to present/convert this static info into a dynamic solution. eg. either custom post types that some how can change the sidebar widgets per post, or via a basic page with custom meta boxes which I know I can already specifically widgitize for each individual artist. See the problem with pages is there is no way to restrict the meta boxes to one form of page creation, and with CPT's theres no easy way to present individual sidebars for each artist. –  Mr.Brown May 2 '12 at 14:21
    
@brasafilo - in a nutshell, I would be presenting basic bits of info such as the_title_attribute, the_content, the_post_thumbnail, and then additional custom meta boxes....via either method (CPT or Page) –  Mr.Brown May 2 '12 at 14:23
1  
@Mr.Brown - updated the answer with 2 links that can be useful when defining your route –  brasofilo May 2 '12 at 14:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.