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.

Is there no post AND page parameter for WordPress functions? So would I have to create a custom post type to define post AND page?

For example, add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args ); you need to define what type of $post_type. The options are 'post', 'page', 'link' or a custom post type.

I'd like to define a meta box that applies to both the post and page. Surely copying and pasting the function and then replacing the $post_type with post and page would work but it wouldn't be very efficient.

I know that is_singular() defines both a post and page, but that is for a query.

Any ideas? I'd prefer not to install another plugin. One reason I am creating a meta box is so that I don't have to install a plugin.

share|improve this question
    
You can't define a POST and PAGE custom type. By definition, a page supports hierarchy and a post doesn't. Incidentally, this is the major difference between post and page. I don't understand why calling add_meta_box twice is not efficient - though a bit more code. –  KDM Jun 20 '12 at 3:59

1 Answer 1

up vote 0 down vote accepted

There's no reason you can't call add_meta_box() twice in a row and use the same function to display it both times; just change the $post_type parameter.

Example

foreach ( array ( 'post', 'page' ) as $post_type )
{
    add_meta_box( 'your_id', 'your title', 'your_callback', $post_type );
}
share|improve this answer
    
I just try to make my code as DRY (Don't Repeat Yourself) as possible. For me, functions should be something that can be executed without having to repeat adding the code. –  AAA Jun 20 '12 at 3:58
    
@Aimee Yeah i feel you.. but sometimes in wordpress or other peoples' frameworks in general, things don't always work out the exact way you would do it. I would say that in order to keep this as DRY as possible, you could define all the parameters in variables and then pass the same variables to each add_meta_box call. Then if you need to change anything, you only need to change it one place. –  jessica Jun 20 '12 at 4:07
1  
@Aimee I have added a code example to show how easy this is. :) –  toscho Jun 20 '12 at 8:09

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.