0

I have added some fields to a custom post type through

function add_electric_portfolio_box() {
        add_meta_box(
                "electric_portfolio_info", __('Detalles de la web'), "electric_portfolio_fields", "electric_portfolio", "normal"
        );    
}

Which I then call with:

add_action('admin_init', 'add_electric_portfolio_box');

But now those custom fields appear in regular pages too. Is it posible to show those fields ONLY on that type of post?

Here is a screenshot of the undesired fields in the regular pages Undesired fields

as opposed to the desired fields in the custom post: enter image description here

1 Answer 1

1

Sure, just check the post type of the post you're on.

global $post; // make sure $post is available
if( get_post_type( $post ) == 'your_custom_post_type's_name' ) {
    add_meta_box( ... );
}

UPDATE: I think the $post variable might not have been getting passed to get_post_type.

3
  • get_post_type returns false. Maybe it is because of the way I add the meta box? (Check out my edits)
    – Cmorales
    Jul 24, 2012 at 20:11
  • Try the updated snippet.
    – mrwweb
    Jul 24, 2012 at 20:47
  • I keep getting false :S By the way... I add the post type on a plugin, maybe that's the reason?
    – Cmorales
    Jul 25, 2012 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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