简体   繁体   English

向页面添加默认自定义字段

[英]Adding Default custom fields to pages

I try to add some default custom fields only to the pages in wordpress with this code at my functions.php: 我尝试在我的functions.php中使用此代码将一些默认的自定义字段仅添加到wordpress中的页面:

// Adding Custom Fields
add_action('save_post', 'set_default_custom_fields', '99');
function set_default_custom_fields($post_id){
    if ( $_GET['post_type'] == 'page' ) {

        add_post_meta($post_id, 'Header', '', true);
        add_post_meta($post_id, 'Überschrift', '', true);

    }
}

The custom fields are showing by adding a new page but not if I EDIT a existing page. 通过添加新页面显示自定义字段,但是如果我编辑现有页面则不显示。 If a page is still there and "published" it will not be showing. 如果页面仍然存在并“发布”,则不会显示。 Whats wrong with my code? 我的代码有什么问题?

You can also use Advanced Custom Fields. 您也可以使用“高级自定义字段”。 It's a free plugin with adds custom fields to any post_type, so also to pages. 这是一个免费插件,可将自定义字段添加到任何post_type,也可以添加到页面。

http://advancedcustomfields.com It can also be implemented into your php code. http://advancedcustomfields.com也可以在您的php代码中实现。 It works really great! 它真的很棒!

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM