在Wordpress中使用2种不同的自定义帖子类型的方法。

时间:2014-01-04 19:39:20

标签: php wordpress

所以我遇到了一个我没有编码的主题的问题。这是我正在谈论的2个代码块。

代码块#1:

    function custom_classes_post_type() {
   $labels = array(
        'name' => __( 'Classes' ),
        'singular_name' => __( 'Class' ),
        'all_items' => __( 'All Classes' ),
        'add_new' => __( 'Add New Class' ),
        'add_new_item' => __( 'Add New Class' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Class' ),
        'new_item' => __( 'New Class' ),
        'view' => __( 'View Class' ),
        'view_item' => __( 'View Class' ),
        'search_items' => __( 'Search Classes' ),
        'not_found' => __( 'No Classes found' ),
        'not_found_in_trash' => __( 'No Classes found in Trash' ),
        'parent' => __( 'Parent Class' ),
      );
  $args = array(
          'labels'        => $labels,
          'description'   => 'All of our Classes are contained here',
          'public'        => true,
          'menu_position' => 5,
          'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
      );  
      register_post_type( 'Classes', $args );  
}
add_action( 'init', 'custom_classes_post_type' );

function my_taxonomies_classes() {
  $labels = array(
    'name'              => _x( 'Class Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Class Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Class Categories' ),
    'all_items'         => __( 'All Class Categories' ),
    'parent_item'       => __( 'Parent Class Category' ),
    'parent_item_colon' => __( 'Parent Class Category:' ),
    'edit_item'         => __( 'Edit Class Category' ), 
    'update_item'       => __( 'Update Class Category' ),
    'add_new_item'      => __( 'Add New Class Category' ),
    'new_item_name'     => __( 'New Class Category' ),
    'menu_name'         => __( 'Class Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'class_category', 'classes', $args );
}
add_action( 'init', 'my_taxonomies_classes', 0 );

代码块#2(旧的我没做过)

    /** Custom Post Types */
add_action( 'init', 'create_post_type' );

function create_post_type() {
  register_post_type( 'classes', 
    array(
      'labels' => array(
        'name' => __( 'Classes' ),
        'singular_name' => __( 'Class' ),
        'all_items' => __( 'All Classes' ),
        'add_new' => __( 'Add New Class' ),
        'add_new_item' => __( 'Add New Class' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Class' ),
        'new_item' => __( 'New Class' ),
        'view' => __( 'View Class' ),
        'view_item' => __( 'View Class' ),
        'search_items' => __( 'Search Classes' ),
        'not_found' => __( 'No Classes found' ),
        'not_found_in_trash' => __( 'No Classes found in Trash' ),
        'parent' => __( 'Parent Class' ),
      ),
      'description' => __( 'A custom post type for creating and organizing classes.' ),
      'public' => true,
      'show_ui' => true,
      'publicly_queryable' => true,
      'exclude_from_search' => false,
      'hierarchical' => true,
      'query_var' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'trackbacks', 'revisions', 'author', 'page-attributes', 'excerpt', 'custom-fields', 'thumbnail' ),
      'rewrite' => array( 'slug' => 'classes', 'with_front' => false ),
      'taxonomies' => array( 'post_tag'),
      'can_export' => true,
    )
  );

    register_post_type( 'jummah-khutba', 
    array(
      'labels' => array(
        'name' => __( 'Jummah Khutba' ),
        'singular_name' => __( 'Jummah Khutba' ),
        'all_items' => __( 'All Khutbas' ),
        'add_new' => __( 'Add Khutba' ),
        'add_new_item' => __( 'Add Khutba' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Khutba' ),
        'new_item' => __( 'New Khutba' ),
        'view' => __( 'View Khutba' ),
        'view_item' => __( 'View Khutba' ),
        'search_items' => __( 'Search Khutbas' ),
        'not_found' => __( 'No Khutbas found' ),
        'not_found_in_trash' => __( 'No Khutbas found in Trash' ),
        'parent' => __( 'Parent Khutba' ),
      ),
      'description' => __( 'A custom post type for creating and organizing Jummah Khutbas.' ),
      'public' => true,
      'show_ui' => true,
      'publicly_queryable' => true,
      'exclude_from_search' => false,
      'hierarchical' => true,
      'query_var' => true,
      'has_archive' => true,
      'supports' => array( 'title', 'editor', 'trackbacks', 'revisions', 'author', 'page-attributes', 'excerpt', 'custom-fields', 'thumbnail' ),
      'rewrite' => array( 'slug' => 'khutba', 'with_front' => false ),
      'taxonomies' => false,
      'can_export' => true,
    )
  );

}
add_action( 'init', 'create_post_type' );

如果我使用第二个代码块,我会收到一个Wordpress错误: 警告:在第1334行的/home/content/55/12189655/html/wp-includes/post.php中为foreach()提供的参数无效

但是,如果我使用代码块#1,那么该错误就会消失,但渲染页面不会显示所有自定义帖子类型。它实际上显示为空。代码块#1也没有列出自定义帖子类型类的“标签”。

我只是不确定我需要做些什么来使一切正常。任何和所有的帮助表示赞赏。

提前致谢!

1 个答案:

答案 0 :(得分:0)

c尝试更改为以下代码。您还需要在更改后刷新永久链接设置。

function custom_classes_post_type() {
   $labels = array(
        'name' => __( 'Classes' ),
        'singular_name' => __( 'Class' ),
        'all_items' => __( 'All Classes' ),
        'add_new' => __( 'Add New Class' ),
        'add_new_item' => __( 'Add New Class' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Class' ),
        'new_item' => __( 'New Class' ),
        'view' => __( 'View Class' ),
        'view_item' => __( 'View Class' ),
        'search_items' => __( 'Search Classes' ),
        'not_found' => __( 'No Classes found' ),
        'not_found_in_trash' => __( 'No Classes found in Trash' ),
        'parent' => __( 'Parent Class' ),
      );
  $args = array(
          'labels'        => $labels,
          'description' => __( 'A custom post type for creating and organizing classes.' ),
          'public' => true,
          'show_ui' => true,
          'publicly_queryable' => true,
          'exclude_from_search' => false,
          'hierarchical' => true,
          'query_var' => true,
          'has_archive' => true,
          'supports' => array( 'title', 'editor', 'trackbacks', 'revisions', 'author', 'page-attributes', 'excerpt', 'custom-fields', 'thumbnail' ),
          'rewrite' => array( 'slug' => 'classes', 'with_front' => false ),
          'taxonomies' => array( 'post_tag'),
          'can_export' => true,'comments', 'custom-fields' ),
      );  
      register_post_type( 'Classes', $args );  
}
add_action( 'init', 'custom_classes_post_type' );

function my_taxonomies_classes() {
  $labels = array(
    'name'              => _x( 'Class Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Class Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Class Categories' ),
    'all_items'         => __( 'All Class Categories' ),
    'parent_item'       => __( 'Parent Class Category' ),
    'parent_item_colon' => __( 'Parent Class Category:' ),
    'edit_item'         => __( 'Edit Class Category' ), 
    'update_item'       => __( 'Update Class Category' ),
    'add_new_item'      => __( 'Add New Class Category' ),
    'new_item_name'     => __( 'New Class Category' ),
    'menu_name'         => __( 'Class Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'class_category', 'classes', $args );
}
add_action( 'init', 'my_taxonomies_classes', 0 );