【问题标题】:how to display custom fields from custom post types如何显示自定义帖子类型的自定义字段
【发布时间】:2021-11-26 06:45:23
【问题描述】:

我使用“高级自定义字段”和“自定义帖子类型 UI”插件来创建自定义帖子类型和自定义字段。我使用它们,它们为我工作。我创建了一个名为“视频”的新帖子类型,并为它打开了存档页面,并创建了我需要的自定义字段(我只做 URL 输入),还创建了名为“播放列表”的分类,并且一切顺利。但是现在,当我转到视频的单页时,我看不到帖子中的内容,没有 URL,没有视频平台名称,也没有出现数据。我看不到任何自定义字段。另外,我想设计视频的存档页面。

我正在使用 WPBakery 页面生成器。我在互联网和 youtube 上搜索,找到了使用 Elementor 的解决方案,但我没有找到任何使用 WPBakery 的解决方案。有人可以帮忙吗?

创建自定义帖子类型的插件代码:

视频帖子类型:

function cptui_register_my_cpts() {

    /**
     * Post Type: Videos.
     */

    $labels = [
        "name" => __( "Videos", "custom-post-type-ui" ),
        "singular_name" => __( "Video", "custom-post-type-ui" ),
        "featured_image" => __( "Thumbnail Image", "custom-post-type-ui" ),
        "set_featured_image" => __( "Set Thumbnail Image", "custom-post-type-ui" ),
        "remove_featured_image" => __( "Remove Thumbnail Image", "custom-post-type-ui" ),
        "use_featured_image" => __( "Use Thumbnail Image", "custom-post-type-ui" ),
    ];

    $args = [
        "label" => __( "Videos", "custom-post-type-ui" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "delete_with_user" => false,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => [ "slug" => "videos", "with_front" => true ],
        "query_var" => true,
        "menu_icon" => "dashicons-format-video",
        "supports" => [ "title", "editor", "thumbnail" ],
        "show_in_graphql" => false,
    ];

    register_post_type( "videos", $args );
}

add_action( 'init', 'cptui_register_my_cpts' );

播放列表分类:

function cptui_register_my_taxes_playlist() {

    /**
     * Taxonomy: Playlists.
     */

    $labels = [
        "name" => __( "Playlists", "custom-post-type-ui" ),
        "singular_name" => __( "Video Playlist", "custom-post-type-ui" ),
    ];

    
    $args = [
        "label" => __( "Playlists", "custom-post-type-ui" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => false,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'playlist', 'with_front' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "show_tagcloud" => true,
        "rest_base" => "playlist",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => false,
        "show_in_graphql" => false,
    ];
    register_taxonomy( "playlist", [ "videos" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_playlist' );

自定义字段:

        if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array(
    'key' => 'group_615b37c91ca00',
    'title' => 'Videos',
    'fields' => array(
        array(
            'key' => 'field_615b37f59afc2',
            'label' => 'Video URL',
            'name' => 'video_url',
            'type' => 'url',
            'instructions' => 'insert YouTube url.',
            'required' => 1,
            'conditional_logic' => 0,
            'wrapper' => array(
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'default_value' => '',
            'placeholder' => 'https://youtu.be/******',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'videos',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => true,
    'description' => '',
));

endif;      

【问题讨论】:

  • “我看不到任何自定义字段” - 你到底做了什么让它们出现?你没想到这会自动发生,是吗……?

标签: php wordpress wordpress-theming custom-wordpress-pages wordpress-gutenberg


【解决方案1】:

我认为您需要为视频帖子类型创建一个文件。您可以轻松地在您的主题文件夹中创建名称 must single-videos.php。单个帖子模板代码示例想法分享如下。

<?php
get_header();
?>
<h1><?php the_title(); ?></h1>
<div class="content">
    <?php the_content(); ?>
    <span><?php the_field('video_url'); ?></span>
</div>
<?php
get_footer();
?>

我希望你能看到你所期望的东西。 注意:必须是flush permalink。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多