自定义字段未保存在WordPress中

时间:2018-04-06 作者:mad2kx

before I start, I am not using any plugins for my CPT and Custom Fields. I do not want to make use of such plugins.

我正在使用几种自定义帖子类型,并在创建的帖子中包含自定义字段。出于某种奇怪的原因,在视频下创建的自定义字段将不会保存。每次我都要用相同的名称创建一个新的自定义字段,并再次添加内容。我无法在该下拉列表中选择相应的自定义字段。

自定义字段及其内容存储在Wordpress数据库中,但Wordpress在创建新的自定义帖子时会“忘记”它们。

我创建了CPT,如下所示:

// Video Custom Post Type
function video_init() {
    // set up product labels
    $labels = array(
        \'name\' => \'Videos\',
        \'singular_name\' => \'Video\',
        \'add_new\' => \'Add New Video\',
        \'add_new_item\' => \'Add New Video\',
        \'edit_item\' => \'Edit Video\',
        \'new_item\' => \'New Video\',
        \'all_items\' => \'All Videos\',
        \'view_item\' => \'View Video\',
        \'search_items\' => \'Search Videos\',
        \'not_found\' =>  \'No Videos Found\',
        \'not_found_in_trash\' => \'No Videos found in Trash\', 
        \'parent_item_colon\' => \'\',
        \'menu_name\' => \'Video\',
    );

    // register post type
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'has_archive\' => true,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'rewrite\' => array(\'slug\' => \'video\'),
        \'query_var\' => true,
        \'menu_icon\' => \'dashicons-video-alt3\',
        \'supports\' => array(
            \'title\',
            \'editor\',
            \'custom-fields\',
        )
    );
    register_post_type( \'video\', $args );

    // register taxonomy
    register_taxonomy(\'video_category\', \'video\', array(\'hierarchical\' => true, \'label\' => \'Category\', \'query_var\' => true, \'rewrite\' => array( \'slug\' => \'video-category\' )));
}
add_action( \'init\', \'video_init\' );
?>
那么我包括youtubelinksubtitle 进入我的岗位enter image description here

最后,我给CPT打电话,包括它的post meta,如下所示

<!--VIDEOS-->
    <div id="videos" class="block relative" style="z-index: 90;padding-top: 100px;padding-bottom: 100px; margin-top: 50% !important;">
        <h3 class="block">v/deos > </h3>
        <?php $query = new WP_Query( array( \'post_type\' => \'video\' ) ); ?>
        <?php 

                    $i = 1;
                    ?>
        <?php if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <?php if(($i % 2) == 0) { ?>
        <div class="full-width text-left big-big-block video-cnt">
            <div class="row">
                <div class="col-sm-8" style="width: 100%; height: 100% auto">
                    <?php if (!((get_post_meta($post->ID, \'youtubelink\', TRUE))==\'\')) {
                                        echo wp_oembed_get( get_post_meta($post->ID, "youtubelink", true) );
                                        }?>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-5 col-sm-offset-7 text-left video-description-text">
                    <h3>
                        <?php the_title(); ?><br/>
                        <small><?php echo get_post_meta($post->ID, \'subtitle\', true); ?> //////////////////</small></h3>
                    <div class="block">
                        <p>
                            <?php the_content(); ?>
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <?php } elseif (($i % 2) !== 0) { ?>
        <div class="full-width text-left big-big-block video-cnt">
            <div class="row">
                <div class="col-sm-8 col-sm-offset-4">
                    <?php if (!((add_post_meta($post->ID, \'youtubelink\', TRUE))==\'\')) {
                                        echo wp_oembed_get( get_post_meta($post->ID, "youtubelink", true) );
                                        }?>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-5 text-right video-description-text">
                    <h3>
                        <?php the_title(); ?><br/>
                        <small><?php echo add_post_meta($post->ID, \'subtitle\', true); ?> //////////////////</small></h3>
                    <div class="block">
                        <p>
                            <?php the_content(); ?>
                        </p>
                    </div>
                </div>
            </div>
            <?php } ?>
            <?php $i++; ?>
            <?php endwhile; ?>
            <?php endif; ?>
            <!-- show 404 error here -->
        </div>
    </div>
所以现在,当我创建一个新的视频CPT时,我必须创建youtubelinksubtitle 再一次这让我很沮丧,因为一切都很完美,只是在这个特定的CPT下,我不明白为什么我必须一次又一次地创建元字段。

1 个回复
最合适的回答,由SO网友:Milo 整理而成

默认情况下,post meta表单仅限于显示30个键。要更改该值,请使用postmeta_form_limit filter:

add_filter( \'postmeta_form_limit\', function( $limit ) {
    return 50;
} );
还可以考虑添加custom meta box, 您可以在其中设置专用字段,而无需每次都选择一个键。

结束

相关推荐