如何在WordPress中创建自定义帖子类型?

如何在WordPress中创建自定义帖子类型?,wordpress,custom-post-type,Wordpress,Custom Post Type,我想知道如何使用WordPress创建自定义帖子类型的过程。如果有人能帮助我完成代码和插件的完整过程,我将不胜感激。我还将写一篇关于这个问题的博文,我需要Stackoverflow顶级贡献者的帮助。将类似的代码添加到您的函数中。php add_action( 'init', 'create_post_type' ); function create_post_type() { register_post_type( 'your_custom_name', array(

我想知道如何使用WordPress创建自定义帖子类型的过程。如果有人能帮助我完成代码和插件的完整过程,我将不胜感激。我还将写一篇关于这个问题的博文,我需要Stackoverflow顶级贡献者的帮助。

将类似的代码添加到您的
函数中。php

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'your_custom_name',
        array(
            'labels' => array(
                'name' => __( 'Custom_names' ),
                'singular_name' => __( 'Custom_names' )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );
}
之后,您可以在dashbooard左栏中看到另一个选项,用于添加自定义帖子。

您可以使用下面的WordPress脚本创建自定义帖子类型,而无需任何插件*/

  $labels = array(
    'name' => _x('Team', 'Team', 'Team') ,
    'singular_name' => _x('Team', 'Team', 'rjis') ,
    'menu_name' => _x('Meet the Team', 'admin menu', 'rjis') ,
    'name_admin_bar' => _x('Team', 'add new on admin bar', 'rjis') ,
    'add_new' => _x('Add New', 'Team', 'rjis') ,
    'add_new_item' => __('Add New Team', 'rjis') ,
    'new_item' => __('New Team', 'rjis') ,
    'edit_item' => __('Edit Team', 'rjis') ,
    'view_item' => __('View Team', 'rjis') ,
    'all_items' => __('All Team Members', 'rjis') ,
    'search_items' => __('Search Team', 'rjis') ,
    'parent_item_colon' => __('Parent Team:', 'rjis') ,
    'not_found' => __('No Team found.', 'rjis') ,
    'not_found_in_trash' => __('No Team found in Trash.', 'rjis')
  );
  $args = array(
    'labels' => $labels,
    'description' => __('Description.', 'Meet the team') ,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'team'
    ) ,
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array(
      'title',
      'editor',
      'author',
      'thumbnail',
      'excerpt'
    ) ,
    'menu_icon' => PLUGIN_URL . "images/team.png"
  );

 register_post_type('team', $args);

此处,职业词用于PostType的给定名称。您可以通过更改Carrer名称来创建多个帖子类型

//职业自定义帖子
$labels=数组(
'姓名'=>'职业',
“单数姓名”=>“职业”,
'add_new'=>'add career',
“添加新项目”=>“添加新职业”,
“编辑项目”=>“编辑职业生涯”,
“新项目”=>“新职业”,
“所有项目”=>“所有职业”,
“查看项目”=>“查看职业”,
“搜索项目”=>“搜索职业”,
“未找到”=>“未找到职业”,
“在垃圾桶中找不到工作”=>“在垃圾桶中找不到工作”,
“父项冒号”=>“”,
“菜单名称”=>“职业”
);
$args=数组(
“标签”=>$labels,
“public”=>正确,
“公开可查询”=>正确,
'show_ui'=>true,
“在菜单中显示”=>true,
'query_var'=>true,
“重写”=>正确,
“能力类型”=>“职位”,
“has_archive”=>true,
“分层”=>false,
“菜单位置”=>null,
“分类法”=>array('career_cat'),
'支持'=>数组(
“头衔”,
"编辑",,
"摘录",,
“缩略图”,
“自定义字段”
)
);
注册职位类型('career',$args);
$labels=数组(
'name'=>'career_Category',
“单数姓名”=>“职业”,
“搜索项目”=>“搜索职业类别”,
“所有项目”=>“所有职业类别”,
“家长项目”=>“家长职业类别”,
“父项冒号”=>“父项职业类别:”,
“编辑项目”=>“编辑职业类别”,
“更新项目”=>“更新职业类别”,
“添加新项目”=>“添加新职业类别”,
“新建项目名称”=>“新建类别名称”,
“菜单名称”=>“职业类别”
);
$args=数组(
“标签”=>$labels,
“分层”=>正确,
“重写”=>false,
“功能”=>array('manage_terms')
);
注册分类法('career\u cat',数组('career'),$args);
添加主题支持(“后期缩略图”,数组(“职业”);
设置大小(100100);

Full code hear:稍微解释一下你在这里所做的事情会非常好。至少有几个代码注释?答案的格式中有一些问题需要修正
## This is my full working code for creating custom post types ##
    function slide_init()
    {
        $labels = array(
            'name'                  => 'Slider',
            'singular_name'         => 'Slider',
            'menu_name'             => 'Slider',
            'name_admin_bar'        => 'Slider',
            'add_new'               => 'Add New Slider',
            'add_new_item'          => 'Add New Slider',
            'new_item'              => 'New Slider',
            'edit_item'             => 'Edit Slider',
            'view_item'             => 'View Slider',
            'all_items'             => 'All Slider Images',
            'search items'          => 'Search Slider',
            'parent_item_colon'     => 'Parent Slider',
            'not_found'             => 'No Slider Found',
            'not_found_in_trash'    => 'No Slider Found In Trash',
            );

        $args = array(
            'labels'                => $labels,
            'public'                => true,
            'publicaly_queryable'   => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'query_var'             => true,
            'rewrite'               => array( 'slug' => 'Slider' ),
            'capability_type'       => 'post',
            'has_archive'           => true,
            'Hierarchical'          => false,
            'menu_position'         => null,
            'menu_icon'             => 'dashicons-format-image',
            'supports'              => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments','hierarchical','trackbacks','custom-fields','revisions','page-attributes'),
            'taxonomies'            =>array('category'),
            );

        register_post_type('Slider',$args);
        register_taxonomy('Slider_category','Slider',array('hierarchical' => true, 'label' => 'Category', 'query_var' => true, 'rewrite' => array('slug' => 'slider-category')));
    }
    add_action('init','slide_init');
  /**  Meet the team custom post 
  $labels = array(
    'name' => _x('Team', 'Team', 'Team') ,
    'singular_name' => _x('Team', 'Team', 'rjis') ,
    'menu_name' => _x('Meet the Team', 'admin menu', 'rjis') ,
    'name_admin_bar' => _x('Team', 'add new on admin bar', 'rjis') ,
    'add_new' => _x('Add New', 'Team', 'rjis') ,
    'add_new_item' => __('Add New Team', 'rjis') ,
    'new_item' => __('New Team', 'rjis') ,
    'edit_item' => __('Edit Team', 'rjis') ,
    'view_item' => __('View Team', 'rjis') ,
    'all_items' => __('All Team Members', 'rjis') ,
    'search_items' => __('Search Team', 'rjis') ,
    'parent_item_colon' => __('Parent Team:', 'rjis') ,
    'not_found' => __('No Team found.', 'rjis') ,
    'not_found_in_trash' => __('No Team found in Trash.', 'rjis')
  );
  $args = array(
    'labels' => $labels,
    'description' => __('Description.', 'Meet the team') ,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'query_var' => true,
    'rewrite' => array(
      'slug' => 'team'
    ) ,
    'capability_type' => 'post',
    'has_archive' => false,
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array(
      'title',
      'editor',
      'author',
      'thumbnail',
      'excerpt'
    ) ,
    'menu_icon' => PLUGIN_URL . "images/team.png"
  );

 register_post_type('team', $args);
<?php
require_once 'wp-load.php'; //path of wp-load.php

$name = 'my post';
$content = 'dummy Content here';
$featured_image = 'fullimage url.jpg'; //pass here full image url

$post_data = array(
    'post_title'    => wp_strip_all_tags( $name ),
    'post_content'  => $content,
    'post_status'   => 'publish',
    'post_type'     => 'post',
    'post_author'   => 1,
    'post_category' => array(1,2),
    'page_template' => ''
);
$post_id = wp_insert_post( $post_data, $error_obj );

// for custom field
//add_post_meta( $post_id, 'post_date', 'Dec 5, 2018' ); // second parameter is your custom field name, 3rd parameter is value

generate_Featured_Image($featured_image, $post_id );

function generate_Featured_Image( $image_url, $post_id  ){
    $upload_dir = wp_upload_dir();
    $image_data = file_get_contents($image_url);
    $filename = basename($image_url);
    if(wp_mkdir_p($upload_dir['path']))
        $file = $upload_dir['path'] . '/' . $filename;
    else
        $file = $upload_dir['basedir'] . '/' . $filename;
    file_put_contents($file, $image_data);

    $wp_filetype = wp_check_filetype($filename, null );
    $attachment = array(
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => sanitize_file_name($filename),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
    $res1= wp_update_attachment_metadata( $attach_id, $attach_data );
    $res2= set_post_thumbnail( $post_id, $attach_id );
    echo 'post created...';
}
?>