【问题标题】:How to create custom post types in WordPress?如何在 WordPress 中创建自定义帖子类型?
【发布时间】:2014-03-25 16:37:07
【问题描述】:

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

【问题讨论】:

标签: wordpress custom-post-type


【解决方案1】:

这里职业词用于 PostType 的名字。您可以制作多个帖子类型,只需更改 Carrer Name。

// career CUSTOM POST
$labels = array(
        'name' => 'career',
        'singular_name' => 'career',
        'add_new' => 'Add career',
        'add_new_item' => 'Add New career',
        'edit_item' => 'Edit career',
        'new_item' => 'New career',
        'all_items' => 'All career',
        'view_item' => 'View career',
        'search_items' => 'Search career',
        'not_found' =>  'No career found',
        'not_found_in_trash' => 'No career found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'career'
    );
 $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'taxonomies' => array('career_cat'),
        'supports' => array(
            'title',
            'editor',
      'excerpt',
            'thumbnail',
  'custom-fields'
        )
    );
 register_post_type('career', $args);
    $labels = array(
        'name' => 'career_Category',
        'singular_name' => 'career',
        'search_items' =>  'Search career_Categories',
        'all_items' => 'All career_Categories',
        'parent_item' => 'Parent career_Category',
        'parent_item_colon' => 'Parent career_Category:',
        'edit_item' => 'Edit career_Category',
        'update_item' => 'Update career_Category',
        'add_new_item' => 'Add New career_Category',
        'new_item_name' => 'New Category Name',
        'menu_name' => 'career_Categories'
    );
 $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'rewrite' => false,
        'capabilities' => array('manage_terms')
    );
    register_taxonomy('career_cat', array('career'), $args);
    add_theme_support('post-thumbnails', array('career'));
    set_post_thumbnail_size(100, 100);

【讨论】:

  • 答案格式有些问题
【解决方案2】:
<?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...';
}
?>

【讨论】:

    【解决方案3】:
      /**  Meet the team custom post 
    

    您可以使用以下 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);
    

    【讨论】:

      【解决方案4】:
      ## 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');
      

      【讨论】:

        【解决方案5】:
        function my_custom_event() {
          $labels = array(
            'name'               => _x( 'Events', 'post type general name' ),
            'singular_name'      => _x( 'Events', 'post type singular name' ),
            'add_new'            => _x( 'Add New', 'Events' ),
            'add_new_item'       => __( 'Add New Events' ),
            'edit_item'          => __( 'Edit Events' ),
            'new_item'           => __( 'New Events' ),
            'all_items'          => __( 'All Events' ),
            'view_item'          => __( 'View Events' ),
            'search_items'       => __( 'Search Events' ),
            'not_found'          => __( 'No Events found' ),
            'not_found_in_trash' => __( 'No Events found in the Trash' ), 
            'parent_item_colon'  => '',
            'menu_name'          => 'Events'
        );
        
        $args = array(
            'labels'        => $labels,
            'description'   => 'Events',
            'public'        => true,
            'show_ui'        => true,
            'capability_type'  => 'post',
            'menu_position' => 5,
            'supports'      => array( 'title' , 'thumbnail', 'editor', 'page-attributes'),
            'has_archive'   => true,
        );   
        
        register_post_type( 'event', $args );   
        }
        add_action( 'init', 'my_custom_event' );
        

        【讨论】:

        • 稍微叙述一下你在这里所做的事情会非常好。至少有几个代码 cmets?
        【解决方案6】:

        将与此类似的代码添加到您的functions.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,
                )
            );
        }
        

        之后,您可以在仪表板左侧栏中看到另一个选项来添加自定义帖子。 see more about Post Types

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-05-13
          • 2020-06-06
          • 2015-11-06
          • 2012-10-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-17
          相关资源
          最近更新 更多