Wordpress - 将自定义字段添加到帖子屏幕 [英] Wordpress - Adding custom field to the post screen

查看:34
本文介绍了Wordpress - 将自定义字段添加到帖子屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 wordpress 中的所有帖子中添加一个小框,例如摘录"框,以便我可以输入 URL.

I was wondering if its possible to add a little box, like the 'excerpt' box to all posts in wordpress so that i can enter a URL.

干杯,

推荐答案

您可以使用 add_meta_box 函数.您还需要一个在帖子屏幕上输出表单 html 的回调函数和一个保存函数.

You can use the add_meta_box function. You also need a callback function that outputs the form html on the post screen and a save function.

这是一个基本示例,它在帖子屏幕的右下方添加了一个 URL 元框.

Here is a basic example that adds a URL meta box to lower right hand side of the post screen.

add_action( 'add_meta_boxes', 'c3m_sponsor_meta' );
        function c3m_sponsor_meta() {
                add_meta_box( 'c3m_meta', 'Sponsor URL Metabox', 'c3m_sponsor_url_meta', 'post', 'side', 'high' );
                }

            function c3m_sponsor_url_meta( $post ) {
                $c3m_sponsor_url = get_post_meta( $post->ID, '_c3m_sponsor_url', true);
                echo 'Please enter the sponsors website link below';
                ?>
                <input type="text" name="c3m_sponsor_url" value="<?php echo esc_attr( $c3m_sponsor_url ); ?>" />
                <?php
        }

add_action( 'save_post', 'c3m_save_project_meta' );
        function c3m_save_project_meta( $post_ID ) {
            global $post;
            if( $post->post_type == "post" ) {
            if (isset( $_POST ) ) {
                update_post_meta( $post_ID, '_c3m_sponsor_url', strip_tags( $_POST['c3m_sponsor_url'] ) );
            }
        }
        }

更正了上面代码中的命名空间错误.

Corrected namespace error in code above.

这篇关于Wordpress - 将自定义字段添加到帖子屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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