Wordpress 显示所有帖子类型或页面的自定义元框

Wordpress 显示所有帖子类型或页面的自定义元框,wordpress,Wordpress,我已经创建了一个自定义的帖子类型“产品”,还添加了一些元框。当我添加页面、帖子、菜单和任何帖子类型时,我的元框都会显示在页面顶部的每个地方。我只想显示产品帖子类型 代码在这里 职位类型是产品 请检查我哪里错了。 请帮帮我。试试这个: 将操作“init”更改为“添加元框” 更改如下: add_action( 'admin_init', 'marbel_product_meta_fields' ); 替换为: add_action( 'add_meta_boxes', 'marbel_produc

我已经创建了一个自定义的帖子类型“产品”,还添加了一些元框。当我添加页面、帖子、菜单和任何帖子类型时,我的元框都会显示在页面顶部的每个地方。我只想显示产品帖子类型

代码在这里

职位类型是产品

请检查我哪里错了。 请帮帮我。

试试这个:

将操作“init”更改为“添加元框”

更改如下:

add_action( 'admin_init', 'marbel_product_meta_fields' );
替换为:

add_action( 'add_meta_boxes', 'marbel_product_meta_fields' );
试试这个:

将操作“init”更改为“添加元框”

更改如下:

add_action( 'admin_init', 'marbel_product_meta_fields' );
替换为:

add_action( 'add_meta_boxes', 'marbel_product_meta_fields' );
为了向所有post类型显示元框,可以使用foreach循环在循环结构中迭代post类型

试试这样,它会对你有用的

foreach ( array( 'post', 'page', 'custom_whatever', 'whatever2' ) as $page )
    add_meta_box( 'id', 'whatever', $callback, $page, $context, $priority, $callback_args );
为了向所有post类型显示元框,可以使用foreach循环在循环结构中迭代post类型

试试这样,它会对你有用的

foreach ( array( 'post', 'page', 'custom_whatever', 'whatever2' ) as $page )
    add_meta_box( 'id', 'whatever', $callback, $page, $context, $priority, $callback_args );

删除这个钩子添加动作“保存帖子”,“显示产品”元框“这肯定会有用的

删除此钩子添加操作“保存帖子”、“显示产品”和“元框”这肯定会起作用

有一种解决方案几乎可以在任何地方使用。首先,您需要使用函数在WordPress中的任何位置查找帖子类型:

function marbel_get_post_type ($find = false) {
        global $post, $parent_file, $typenow, $current_screen, $pagenow;
        
        $post_type = NULL;
        
        if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
            $post_type = $post->post_type;

        if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
            $post_type = $current_screen->post_type;

        if(empty($post_type) && !empty($typenow))
            $post_type = $typenow;

        if(empty($post_type) && function_exists('get_current_screen'))
            $post_type = get_current_screen();

        if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
            $post_type = $get_post_type;

        if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
            $post_type = sanitize_key($_REQUEST['post_type']);

        if(empty($post_type) && in_array($pagenow, array('edit.php', 'post-new.php')))
            $post_type = 'post';

        if(is_array($find))
        {
            return in_array($post_type, $find, true);
        }
        else if(is_string($find))
        {
            return ($post_type === $find);
        }
        
        $post_type = apply_filters( 'btnr_get_post_type', $post_type);
        
        return $post_type;
    }
然后你就可以这样做了:

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type('product') ) return;
    /* 
        You can now add metaboxes here 
    */
}
…还是像这样

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type() == 'product' ) return;
    /* 
        You can now add metaboxes here 
    */
}

我给你的功能是我的艺术和平,你可以在任何地方使用它来完成任何你想要的工作。

有一种解决方案,你几乎可以在任何地方使用。首先,您需要使用函数在WordPress中的任何位置查找帖子类型:

function marbel_get_post_type ($find = false) {
        global $post, $parent_file, $typenow, $current_screen, $pagenow;
        
        $post_type = NULL;
        
        if($post && (property_exists($post, 'post_type') || method_exists($post, 'post_type')))
            $post_type = $post->post_type;

        if(empty($post_type) && !empty($current_screen) && (property_exists($current_screen, 'post_type') || method_exists($current_screen, 'post_type')) && !empty($current_screen->post_type))
            $post_type = $current_screen->post_type;

        if(empty($post_type) && !empty($typenow))
            $post_type = $typenow;

        if(empty($post_type) && function_exists('get_current_screen'))
            $post_type = get_current_screen();

        if(empty($post_type) && isset($_REQUEST['post']) && !empty($_REQUEST['post']) && function_exists('get_post_type') && $get_post_type = get_post_type((int)$_REQUEST['post']))
            $post_type = $get_post_type;

        if(empty($post_type) && isset($_REQUEST['post_type']) && !empty($_REQUEST['post_type']))
            $post_type = sanitize_key($_REQUEST['post_type']);

        if(empty($post_type) && in_array($pagenow, array('edit.php', 'post-new.php')))
            $post_type = 'post';

        if(is_array($find))
        {
            return in_array($post_type, $find, true);
        }
        else if(is_string($find))
        {
            return ($post_type === $find);
        }
        
        $post_type = apply_filters( 'btnr_get_post_type', $post_type);
        
        return $post_type;
    }
然后你就可以这样做了:

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type('product') ) return;
    /* 
        You can now add metaboxes here 
    */
}
…还是像这样

function marbel_product_meta_fields() 
{
    if( marbel_get_post_type() == 'product' ) return;
    /* 
        You can now add metaboxes here 
    */
}

我给你的功能是我的艺术和平,你可以在任何地方使用它来完成任何你想要的工作。

谢谢vrajesh,我已经检查了你的建议,但问题仍然存在谢谢vrajesh,我已经检查了你的建议,但问题仍然存在,请尝试删除:添加行动“保存帖子”,“显示产品元框”并将操作“admin_init”替换为“add_meta_box”,非常感谢您Brejesh。它现在可以通过删除添加操作“保存帖子”、“显示产品”和“元框”来工作了。我很高兴它再次适用于您@husainThanks Vrajesh。请尝试删除:添加添加操作“保存帖子”、“显示产品”和“元框”,并将操作“admin init”替换为“添加元框”,非常感谢您Brejesh。通过删除添加操作“保存帖子”、“显示产品”和“元框”,它现在可以工作了。我很高兴它再次适用于你@husainThanks Vrajesh。你好,Naresh,我得到了我的答案。我的代码是在删除这一行后运行的添加_action'admin_init'、'marbel_product_meta_fields';你好,纳雷什,我得到了答案。我的代码是在删除这一行后运行的添加_action'admin_init'、'marbel_product_meta_fields';