How to make this meta box appear in several post types?

1

This is a small fragment of the code, what I want to know is how    make this metabox not only appear in post but also in several post types that I have created. any help?

 $prefix = 'std_';

    $meta_box = array(
        'id' => 'my-meta-box',
        'title' => 'Agregar Opciones',
        'page' => 'post',
        'context' => 'normal',
        'priority' => 'high',
        'fields' => array(
            array(
                'name' => 'Alt',            
                'id' => $prefix . 'alt',
                'type' => 'text',

What I want is to know if there is any way to do it with this code not with others, this works very well only that this metabox appears in post.

Some idea of how to edit the line: 'page' => 'post', to add several custom post type, I have edited and I have not achieved results.

    
asked by Manuel 23.04.2017 в 18:23
source

1 answer

1

If you want to add metaboxes dynamically, the best way would be to use the native Wordpress feature add_meta_box .

You define a $tipos_post arrangement in which you include the types of posts that you want to add the meta_box to, separated by commas and you loop to add the meta_box to each type of post in $tipos_post :

function add_custom_meta_box()
{

    $tipos_post = array ( 'post', 'page', 'tipo_personalizado' );

    foreach( $tipos_post as $tipo_post )
    {
        add_meta_box(
            'custom_meta_box', // $id
            'Custom Meta Box', // $title 
            'show_custom_meta_box', // $callback
             $tipo_post,
            'normal', // $context
            'high' // $priority
        );
    }
}

add_action('add_meta_boxes', 'add_custom_meta_box');

Do not cases with a type of code if there are other functions that could solve the problem better, especially if they are native functions of the CMS you are using.

    
answered by 23.04.2017 в 20:00