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.

I need to post extra information on programs pages, so i have decided to use wordpress metabox but its showing on all pages. So is there any condition check so it could put meta box on those sub pages only where parent_page is 'Programs' or or Parent Page id.

I have already tried this...

function parent_id() {
    global $post;
    if ( is_page() && $post->post_parent ) {
        return $post->post_parent;
    } else {
        return false;
    }
}

if(parent_id() && parent_id() == 15) {
   //register meta box
}

But it didn't work, please help, thanks.

share|improve this question

5 Answers 5

After too many tries i have found following solution myself, but this could save others time.

$pid = (isset($_GET['post']) ? $_GET['post'] : $_POST['post_ID']); 
$page_att = get_page( $pid );
$page_parent = $page_att->post_parent;

if(15 == $page_parent){
//register metabox
}
share|improve this answer

not tested :D

function my_meta_box(){
    global $pagenow,$post;
    $thisParent = $post->post_parent;
    if ($thisParent !='0'){
        $parentTitle = get_the_title($thisParent);
        if ( $pagenow == 'post.php' && $parentTitle =='Programs'){
        // register your meta_box
        }
    }
}
add_action( 'add_meta_boxes', 'my_meta_box' );
share|improve this answer

Possibly a conditional statement like this,

function custom_meta_box() {

    global $post;

    $parent_id = '15';

    if ($post->ID == $parent_id || $post->post_parent == $parent_id) {

        //do your meta-box stuff here

    } else {

        //do something else, or nothing

    }

}

Modified code slightly by instead storing the parent page in a variable $parent_id (or rename to your liking). This way you can set a page ID outside of the conditional statement and should your conditional statement take on any form of complexity you then only need to pass the variable $parent_id around.

Something to note however is that the meta-box will show when you attempt to Add New page because no ID has been assigned to the page yet. Naturally as WordPress does not know ahead of time what type of post you want to create it can't hide the meta-box in the event you might need it.

A better way around this is to create a Custom Post Type to which you can then assign your meta-box to this post type alone. It will never interfere with other posts or pages should you tell it to do so.

share|improve this answer

This is a jQuery solution based on this answer of mine.

The array with the page IDs must be adapted to your needs. There is a console.log being dumped for easy checking.

In this example, the Featured Image meta box is being addressed, #postimagediv. This also has to be adapted to your custom meta box Div ID.

add_action('admin_head', 'wpse_56059_script_enqueuer');
function wpse_56059_script_enqueuer() 
{
    global $current_screen;
    if('page' == $current_screen->id) 
    {
        echo <<<HTML
            <script type="text/javascript">
            jQuery(document).ready( function($) {
                // The page IDs to check for
                var arr = [ "21", "28" ];
                /**
                 * Adjust visibility of the meta box at startup
                */
                value = $('#parent_id').val();
                if($.inArray(value,arr) > -1) {
                    // show the meta box
                    $('#postimagediv').show();
                } else {
                    // hide your meta box
                    $('#postimagediv').hide();
                }

                // Debug only
                // - outputs the ID of the selected page parent
                // - checking for console existance to avoid js errors in non-compliant browsers
                if (typeof console == "object") 
                    console.log ('default value = ' + value);

                /**
                 * Live adjustment of the meta box visibility
                */
                $('#parent_id').live('change', function(){
                    value = $(this).val();
                    if($.inArray(value,arr) > -1) {
                        // show the meta box
                        $('#postimagediv').show();
                    } else {
                        // hide your meta box
                        $('#postimagediv').hide();
                    }

                    // Debug only
                    if (typeof console == "object") 
                        console.log ('live change value = ' + value);
                });                 
            });    
            </script>
HTML;
    }
}
share|improve this answer

Taking a look at the source you can see that the WordPress passes the $post_type and $post object to the action add_meta_boxes as arguments.

So...

add_action('add_meta_boxes', 'wpse56059_conditionally_add_metabox', 10, 2);
function wpse56059_conditionally_add_metabox( $post_type, $post ){
    if(15 == $post->post_parent){
         add_meta_box( 
              'wpse56059_metabox_id',
              __( 'My Metabox', 'wpse56059_textdomain' ),
              'wpse56059_metabox_callback',
              'page' //post-type
          );
    }
}

Alternatively you can use the add_meta_boxes_{post_type} hook (e.g. add_meta_boxes_page for pages) - and this passes the $post object as the only parameter.

share|improve this answer
    
Would it work only on edit screen? –  seoppc Jun 21 '12 at 18:34
    
It'll only add the metabox to the edit screen for the post type 'page' (i've edited it and that can be changed). And it'll only add it if the post has parent '15' –  Stephen Harris Jun 21 '12 at 18:54
    
this check if(15 == $post->post_parent) isn't passing any sub page which has parent page = Programs(15) can we debug it some how to check if $post->post_parent is returning 15 –  seoppc Jun 21 '12 at 21:06
    
wp_die(print_r($post)); just above the if conditional will kill the processing and display the $post object. Then go to a child's edit page - you can then inspect the post_parent value. –  Stephen Harris Jun 22 '12 at 9:46
    
My mistake, the action has 2 arguments - the first being the post type, the second being the post type object! Editing answer... –  Stephen Harris Jun 22 '12 at 9:48

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.