如何显示自定义帖子类型的自定义字段

     2023-02-24     78

关键词:

【中文标题】如何显示自定义帖子类型的自定义字段【英文标题】: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;      

【问题讨论】:

“我看不到任何自定义字段” - 你到底做了什么让它们出现?你没想到这会自动发生,是吗……? 【参考方案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。

【讨论】:

Wordpress - 使用类型为日期的自定义字段在 Elementor 页面中显示帖子

】Wordpress-使用类型为日期的自定义字段在Elementor页面中显示帖子【英文标题】:Wordpress-displaypostsinElementorpageusingcustomfieldwheretypeisdate【发布时间】:2021-01-2803:59:29【问题描述】:我尝试实现这一点,同时尝试应用从这里和那里获... 查看详情

如何在仪表板帖子列表中显示自定义字段(替换标题)

】如何在仪表板帖子列表中显示自定义字段(替换标题)【英文标题】:Howtodisplayacustomfieldinthedashboardpostslist(replacingthetitle)【发布时间】:2019-10-0112:09:49【问题描述】:我正在使用WordPress,我确实创建了一个包含多个自定义字段... 查看详情

通过存档页面上的自定义字段查询自定义帖子类型

】通过存档页面上的自定义字段查询自定义帖子类型【英文标题】:Querycustompoststypebycustomfieldonarchivepage【发布时间】:2017-01-0512:50:33【问题描述】:我在archive-mypostype.php模板中使用以下查询来列出具有特定自定义字段值的自定... 查看详情

仅显示特定自定义帖子类型的自定义分类计数

】仅显示特定自定义帖子类型的自定义分类计数【英文标题】:ShowcountofCustomTaxonomyonlyforaspecificCustomPostType【发布时间】:2018-05-2118:54:09【问题描述】:我想根据特定的自定义帖子类型显示自定义分类的计数。目前,我使用get_term... 查看详情

如何在使用 foreach 循环显示的自定义帖子类型中提供页面的永久链接

】如何在使用foreach循环显示的自定义帖子类型中提供页面的永久链接【英文标题】:Howtogivepermalinkofpageinacustomposttypedisplayedusingforeachloop【发布时间】:2018-02-1409:29:29【问题描述】:我有一个名为服务的自定义帖子类型。在此,... 查看详情

php自定义元字段的自定义帖子类型(代码片段)

查看详情

如何在 wordpress 中显示自定义帖子类型类别?

】如何在wordpress中显示自定义帖子类型类别?【英文标题】:Howtodisplaycustomposttypecategoryinwordpress?【发布时间】:2014-04-1416:13:21【问题描述】:最近我在wordpress中创建了一个自定义帖子类型,并添加了名为category和tags的分类法。... 查看详情

用于显示距当前日期超过 8 个月的 WordPress 自定义帖子类型的自定义查询

】用于显示距当前日期超过8个月的WordPress自定义帖子类型的自定义查询【英文标题】:CustomqueryfordisplayingWordPresscustomposttypesthataremorethan8monthsoldfromcurrentdate【发布时间】:2012-08-1615:56:22【问题描述】:我在Wordpress中的for循环中使... 查看详情

如何从一组自定义字段值中显示 Wordpress 帖子

】如何从一组自定义字段值中显示Wordpress帖子【英文标题】:HowtodisplayWordpresspostsfromanarrayofcustomfieldvalues【发布时间】:2014-08-0205:09:18【问题描述】:我们的网站包含数百个自定义“奖励”帖子。这些帖子中的每一个都包含一个... 查看详情

无法在帖子中保存和显示更新的自定义多选下拉字段

】无法在帖子中保存和显示更新的自定义多选下拉字段【英文标题】:Unabletosaveanddisplayupdatedcustommulti-selectdropdownfieldsinpost【发布时间】:2021-08-1101:50:35【问题描述】:我在woocommerceshop_order页面中显示了一个自定义多选下拉菜单... 查看详情

php通过acf选择字段查询类别中的自定义帖子类型(代码片段)

查看详情

php通过acf选择字段查询类别中的自定义帖子类型(代码片段)

查看详情

php显示wordpress注册的自定义帖子类型列表(代码片段)

查看详情

根据 Wordpress 中的自定义字段值批量重写帖子 slug

】根据Wordpress中的自定义字段值批量重写帖子slug【英文标题】:BulkrewritepostslugsbasedoncustomfieldvalueinWordpress【发布时间】:2012-07-2812:31:37【问题描述】:基本上,我有一个名为“Parts”的自定义帖子类型设置,其中当前包含5,000多... 查看详情

按关系字段 (ACF) 的 Elementor 帖子的自定义查询过滤器

】按关系字段(ACF)的Elementor帖子的自定义查询过滤器【英文标题】:CustomQueryFilterforElementorPostsbyrelationshipfield(ACF)【发布时间】:2021-01-0312:07:05【问题描述】:我有一个名为“艺术家”的自定义帖子类型设置。每个单一艺术家都是... 查看详情

根据 ACF 关系字段显示“相关帖子”

】根据ACF关系字段显示“相关帖子”【英文标题】:Display"RelatedPosts"basedonACFRelationshipField【发布时间】:2016-11-0400:33:15【问题描述】:我想显示单个帖子页面的“相关帖子”,其中包含名为“属性”的自定义帖子类型,... 查看详情

Wordpress:使用 wp_insert_post() 填充自定义帖子类型字段

】Wordpress:使用wp_insert_post()填充自定义帖子类型字段【英文标题】:Wordpress:Usingwp_insert_post()tofillcustomposttypefields【发布时间】:2015-11-0419:20:45【问题描述】:我创建了一个自定义帖子类型wrestling,并使用高级自定义字段创建了... 查看详情

如何在WordPress的自定义帖子类型中附加pdf文件?

】如何在WordPress的自定义帖子类型中附加pdf文件?【英文标题】:HowtoattachthepdffileinthecustomposttypeintheWordPress?【发布时间】:2022-01-1910:45:22【问题描述】:我正在使用WordPress。我创建了一个自定义帖子类型。现在我必须添加文件... 查看详情