如何在 Wordpress 中添加元框? [英] How to add a metabox in Wordpress?

查看:34
本文介绍了如何在 Wordpress 中添加元框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

我使用上面的代码在 Wordpress 中创建了一个元框.但是,为此代码不显示找不到解决方案.请提出有关该问题的任何想法.

解决方案

这完全取决于您希望元框出现的位置.

如果您希望它出现在帖子中,则需要使用

if (!function_exists('add_post_metabox')){函数 add_post_metabox(){add_meta_box('post-meta', esc_html__('My Metabox', 'mytheme'), 'My_Metabox_function', 'post', 'side', 'low');}}add_action('admin_init', 'add_post_metabox');

然后用

创建metabox

if (!function_exists('My_Metabox_function')){函数 My_Metabox_function( $post ){//元框布局和变量在这里}}如果(!function_exists('My_Metabox_save_function')){函数 My_Metabox_save_function($post_id){全球 $post;如果(定义('DOING_AUTOSAVE')&& DOING_AUTOSAVE){返回 $post_id;} 别的{//保存在这里}}}add_action('save_post', 'My_Metabox_save_function');

如果你想要它在页面上,那么你可以像上面那样创建它,但是用页面而不是发布

if (!function_exists('add_page_metabox')){函数 add_page_metabox(){add_meta_box('page-meta', esc_html__('My Metabox on Page', 'mytheme'), 'My_Metabox_page_function', 'page', 'normal', 'high');}}add_action('add_meta_boxes', 'add_page_metabox');

您可以挂钩到 add_meta_boxes 挂钩的 admin_init.请参阅此处了解更多说明.

你可以把它放在你的 functions.php 文件中,或者你可以将它设置在单独的 .php 文件中并在 functions.php 中调用它> 类似:

require_once( get_template_directory(). '/include/metaboxes.php' );

这将包括位于您主题的 /include 目录中的 metaboxes.php.

<?php
function prfx_custom_meta()
{
add_meta_box(
    'some_meta_box_name'
    ,__( 'Some Meta Box Headline', 'plugin_textdomain' )
    ,'render_meta_box_content' //this is callback function.
    ,'post_type'
    ,'advanced'
    ,'high'
);

}
add_action( 'add_meta_boxes', 'prfx_custom_meta' );
?>

I used the above code to create a metabox in Wordpress. But , for that code it doesn't showing up couldn't find the solution. kindly suggest any ideas for that problem .

解决方案

It all depends on where you want the metabox to appear.

If you want it to appear on posts then you need to use

if ( ! function_exists( 'add_post_metabox' ) ){
    function add_post_metabox(){
        add_meta_box('post-meta', esc_html__('My Metabox', 'mytheme'), 'My_Metabox_function', 'post', 'side', 'low');
    }
}

add_action('admin_init', 'add_post_metabox');

And then create metabox with

if ( ! function_exists( 'My_Metabox_function' ) ){
    function My_Metabox_function( $post ){
        //metabox layout and variables here
    }
}

if ( ! function_exists( 'My_Metabox_save_function' ) ){
    function My_Metabox_save_function($post_id){
        global $post;
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
                return $post_id;
            } else{
                //do save here
            }
        }
    }


add_action( 'save_post', 'My_Metabox_save_function' );

If you want it on page then you can create it like above but with page instead of post

if ( ! function_exists( 'add_page_metabox' ) ){
    function add_page_metabox(){
        add_meta_box('page-meta', esc_html__('My Metabox on Page', 'mytheme'), 'My_Metabox_page_function', 'page', 'normal', 'high');
    }
}

add_action('add_meta_boxes', 'add_page_metabox');

You can hook to admin_init of add_meta_boxes hooks. See here for more explanation.

You can put it in your functions.php file or you can set it in separate .php file and call it in functions.php with something like:

require_once( get_template_directory(). '/include/metaboxes.php' );

This will include metaboxes.php that are located in the /include directory of your theme.

这篇关于如何在 Wordpress 中添加元框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆