Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为所有类型的帖子添加wordpress元框_Wordpress - Fatal编程技术网

为所有类型的帖子添加wordpress元框

为所有类型的帖子添加wordpress元框,wordpress,Wordpress,我正在写一个wordpress插件,想为所有类型的帖子添加一个meta_框(“页面”、“帖子”、“自定义帖子类型”)。通过查阅wordpress,我发现它只接受单一类型的帖子。有人知道我要怎么做吗?谢谢 foreach ( array( 'post', 'page', 'custom_whatever', 'whatever2' ) as $page ) add_meta_box( 'id', 'whatever', $callback, $page, $context, $priori

我正在写一个wordpress插件,想为所有类型的帖子添加一个meta_框(“页面”、“帖子”、“自定义帖子类型”)。通过查阅wordpress,我发现它只接受单一类型的帖子。有人知道我要怎么做吗?谢谢

foreach ( array( 'post', 'page', 'custom_whatever', 'whatever2' ) as $page )
    add_meta_box( 'id', 'whatever', $callback, $page, $context, $priority, $callback_args );
请注意,现在$page变成了一个数组,通过它,将获得所有的帖子类型

更新I

您可以使用获取所有已注册的帖子类型,并使用它填充上述数组


(注意:您可能需要筛选并从阵列中获取附件。)

以下是完整的工作代码

<?php

    function my_meta_box() {

        $my_post_types = get_post_types();

        foreach ( $my_post_types as $my_post_type ) {
            add_meta_box( 
                'Meta_box_ID', __( 'Title of the meta box', 'textdomain' ), 'callback_function', $my_post_type
            );
        }
    }

    function callback_function(){

        // Your metabox code goes here :)

        }

    add_action( 'add_meta_boxes', 'my_meta_box' );

?>
为什么这么详细?
每个请求都会调用操作,
$post
对象是全局对象,因此我们可以使用它:

function my_meta_box() {
    global $post;

    add_meta_box(
        "box-id",
        __("Title"), function () {},
        $post->post_type,
        "context");
}

add_action("add_meta_boxes", "my_add_meta_box");

谢谢你,奥伯克。但是我想要一种更抽象的,我不能定义什么是自定义的帖子类型,并且适用于所有的帖子类型。