【问题标题】:How to make custom posts that have custom fields in a WP theme?如何在 WP 主题中制作具有自定义字段的自定义帖子?
【发布时间】:2013-05-09 20:21:59
【问题描述】:

有没有办法在 WP 中创建带有自定义字段(例如标题、导演、0 位或更多演员等)的自定义帖子(例如电影)并使其成为主题的一部分?我猜测需要在functions.php 中添加一些内容,但我不确定如何定义自定义帖子类型并为其分配自定义字段。

如果作为主题的一部分很难做到这一点,我很乐意从管理部分创建它。有什么建议吗?

【问题讨论】:

    标签: wordpress wordpress-theming custom-post-type custom-fields


    【解决方案1】:

    我正在使用More Fields 插件为我的帖子添加自定义字段(默认帖子类型或自定义帖子类型)。

    为了创建自定义帖子类型(当然还有自定义分类),我在functions.php 中添加了一个可以正常工作的代码,如下所示:

            function post_type_aya_bi_aya() {
                $labels = array(
                    'name'               => _x( 'التفاسير', 'post type general name' ),
                    'singular_name'      => _x( '', 'post type singular name' ),
                    'add_new'            => _x( 'أضف تفسير جديد', 'book' ),
                    'add_new_item'       => __( 'أضف تفسير جديد' ),
                    'edit_item'          => __( 'تحرير التفسير' ),
                    'new_item'           => __( 'تفسير جديد' ),
                    'all_items'          => __( 'كافة التفاسير' ),
                    'view_item'          => __( 'مشاهدة التفسير' ),
                    'search_items'       => __( 'بحث في التفاسير' ),
                    'not_found'          => __( 'لا يوجد أي تفسير' ),
                    'not_found_in_trash' => __( 'لا يوجد أي تفسير في سلة المهملات' ), 
                    'parent_item_colon'  => '',
                    'menu_name'          => 'التفاسير'
                );
                $args = array(
                    'labels'        => $labels,
                    'description'   => 'يحنوي على معطيات "آية بآية و التفاسير المرفوعة"',
                    'public'        => true,
                    'menu_position' => 5,
                    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
                    'has_archive'   => true,
                    'rewrite'       => array('slug' => 'aya-bi-aya'),
                );
                register_post_type( 'aya-bi-aya', $args );  
            }
    
     add_action( 'init', 'post_type_aya_bi_aya' );
    
    function taxonomy_aya_bi_aya() {
            $labels = array(
                'name'              => _x( 'السور', 'taxonomy general name' ),
                'singular_name'     => _x( 'السورة', 'taxonomy singular name' ),
                'search_items'      => __( 'بحث في السور' ),
                'all_items'         => __( 'كافة السور' ),
                'parent_item'       => __( '' ),
                'parent_item_colon' => __( '' ),
                'edit_item'         => __( 'تحرير السورة' ), 
                'update_item'       => __( 'تحديث السورة' ),
                'add_new_item'      => __( 'أضف سورة' ),
                'new_item_name'     => __( 'سورة جديدة' ),
                'menu_name'         => __( 'السور' ),
            );
            $args = array(
                'labels' => $labels,
                'hierarchical' => true,
                'public' => TRUE, 
                'show_ui' => TRUE,
                    'query_var' => 'sowar',
    
            );
            register_taxonomy( 'sowar', 'aya-bi-aya', $args );
        }
    
    add_action( 'init', 'taxonomy_aya_bi_aya', 0 );
    

    我在这里解释更多。祝你好运。

    【讨论】:

    • 所以你不能在functions.php中为自定义帖子定义自定义字段?
    • 当然你可以在你的functions.php中。通过代码在 WP 中一切皆有可能。但更好地使用我尝试过的那个插件,它使默认帖子和 CPTS 的 CF 管理变得容易。
    【解决方案2】:

    你也可以使用优秀的Advanced Custom Fields plugin

    这提供了一个快速的管理界面,用于为每个帖子类型/页面模板/页面类型等创建字段,并且有一个非常简单的模板使用 API

    <?php the_field('my_custom_field'); ?>
    

    但我认为您会感兴趣的部分是您可以将字段组导出为 php,然后将它们作为主题的一部分包含在内

    【讨论】:

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