Here is the code, the 5th and final one is not showing up in the ADMIN CP Panel. All the others are working fine, but when I try add a 5th one. Nothing shows up. Not sure if there is a limit or something, does not appear to be from what I have read.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'Slides',
array(
'labels' => array(
'name' => __( 'slides' ),
'singular_name' => __( 'slide' )
),
'public' => true,
'has_archive' => true,
)
);
register_post_type( 'Programs',
array(
'labels' => array(
'name' => __( 'programs' ),
'singular_name' => __( 'program' )
),
'public' => true,
'has_archive' => true,
)
);
register_post_type( 'Boards',
array(
'labels' => array(
'name' => __( 'Boards' ),
'singular_name' => __( 'sponsor' )
),
'public' => true,
'has_archive' => true,
)
);
Your code just work perfectly, you have just forget in your code at the end a closing bracket } and some errors in slug post types (in lowercase):
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'slides', // <== here to lowercase (slug)
array(
'labels' => array(
'name' => __( 'Slides' ), // <== here 1st letter uppercase
'singular_name' => __( 'Slide' ) // <== here 1st letter uppercase
),
'public' => true,
'has_archive' => true,
)
);
register_post_type( 'programs', // <== here to lowercase (slug)
array(
'labels' => array(
'name' => __( 'Programs' ), // <== here 1st letter uppercase
'singular_name' => __( 'Program' ) // <== here 1st letter uppercase
),
'public' => true,
'has_archive' => true,
)
);
register_post_type( 'boards', // <== here to lowercase (slug)
array(
'labels' => array(
'name' => __( 'Boards' ),
'singular_name' => __( 'Board' ) // <== here singular
),
'public' => true,
'has_archive' => true,
)
);
} // <== forgetted this
I have test your code on a test website and even your custom post "Boards" is showing and working:
You may need to flush the rewrite rules by going on backend permalinks settings and simply click on save to regenerate Wordpress rewrite rules related to this new post types…
Is there a limit for post types in WordPress?
There is no limit for number of different custom posts. Try the example below that generates in a very compact code 10 custom posts with a for loop :
add_action( 'init', 'create_post_type' );
function create_post_type() {
$arr = array('abcdef','bcdefg','cdefgh','defghi','efghij','fghijk','ghijkl','hijklm','ijklmn','jklmno');
for( $i = 0; $i < 10; $i++ ) {
$slug = $arr[$i];
$slugs = $arr[$i].'s';
register_post_type( $slug,
array(
'labels' => array(
'name' => $slugs,
'singular_name' => $slug ),
'public' => true,
'has_archive' => true )
);
}
}
References:
Developer WordPress - register_post_type() function
Wordpress Custom Post Types doesn't show in admin left sidebar menu
Related
Created Custom Post Type. The page template for each post is single-speaker.php. But when creating a post and going to its page, it goes to the main page. Can't see page template. And the condition does not work -if(is_singular( 'speaker' )). How can this be fixed?
// create post type
function create_post_type() {
register_post_type( 'speaker',
array(
'labels' => array(
'name' => __( 'Speakers' ),
'singular_name' => __( 'speaker' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'speaker' ),
)
);
register_post_type( 'sessions',
array(
'labels' => array(
'name' => __( 'Sessions' ),
'singular_name' => __( 'sessions' )
),
'public' => true,
'has_archive' => true,
)
);
register_taxonomy(
'positions',
'speaker',
array(
'label' => ( 'Positions' ),
'hierarchical' => true,
)
);
register_taxonomy(
'countries',
'speaker',
array(
'label' => ( 'Countries' ),
'hierarchical' => true,
)
);
}
add_action( 'init', 'create_post_type' );
What you can do first, is to install Query Monitor to check the templates you're landing on and which conditional tags you can use to call this specific template
Try using is_single('speaker') instead
And last, check your loop. Are you calling the main loop or a custom loop for the speaker template
I hope this helps!
I am new in wordpress, i created custom post type and i want this should display only for subscriber only(not for admin ) So how can i do this Here is my current code?
function create_posttype() {
register_post_type( 'UserProject',
array(
'labels' => array(
'name' => __( 'UserProject' ),
'singular_name' => __( 'UserProject' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'UserProject'),
'show_in_rest' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
if ( current_user_can( 'editor' ) && is_user_logged_in()) {
function book_setup_post_type() {
$args = array(
'public' => true,
'label' => __( 'Books', 'textdomain' ),
'menu_icon' => 'dashicons-book',
);
register_post_type( 'book', $args );
}
add_action( 'init', 'book_setup_post_type' );
}
Book is a custom post type you can write your own code here.
Subscribers have no rights to the dashboard. You can set the condition for editor rule or another rule.
I've tried many things to fix this, I'm fairly new to coding a plugin but I can't seem to get this to work.
I have created a custom post type ("Book") perfectly fine. Now I'm trying to add meta-boxes into it. I can't seem to get the coding to connect to each other properly.
I've tried adding the slugs of each meta box to the "support", I've tried various instances of function and add_meta_box as well as putting different things under "register_meta_box_cb" and nothing I'm doing seems to be working.
Any help is appreciated.
// Register Custom Post Type
function post_type_book() {
$args = array(
'label' => __( 'book', 'author_station' ),
'description' => __( 'Custom Book Entry', 'author_station' ),
'supports' => array('title'),
'taxonomies' => array( 'genres', 'series', 'tags' ),
'register_meta_box_cb' => ('as_add_book' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array('slug' => 'Book'),
'capability_type' => 'page',
);
register_post_type( 'as_book', $args );
}
function as_add_book( $meta_boxes ) {
add_meta_box(
'add_meta_boxes',
array( $this, 'as_add_book_boxes' ));
$types = array('post', 'page', 'book');
if (in_array($types)) {
add_meta_box(
'as_add_book',
'add_meta_boxes',
'Book',
'as_add_book_callback',
$types,
'normal',
'high'
);
}
$prefix = 'as_';
$meta_boxes[] = array(
'id' => 'as_add_book',
'title' => esc_html__( 'Book', 'author_station_book' ),
'pages'=> array('as_book'),
'context' => 'advanced',
'priority' => 'high',
'autosave' => 'false',
'fields' => array(
array(
'id' => $prefix . 'book_cover',
'type' => 'image',
'name' => esc_html__( 'Book Cover', 'author_station_book' ),
),
array(
'id' => $prefix . 'book_title',
'type' => 'text',
'name' => esc_html__( 'Title', 'author_station_book' ),
),
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'as_add_book' );
add_action( 'add_meta_boxes', 'my_custom_meta_box' ) );
function my_custom_meta_box(){
$args = array();
add_meta_box(
'my_metabox_id',
__( 'My Meta Box', 'my_textdomain' ), // Title
'my_callback_function', // Callback function that renders the content of the meta box
'post', // Admin page (or post type) to show the meta box on
'side', // Context where the box is shown on the page
'high', // Priority within that context
$args // Arguments to pass the callback function, if any
);
}
function my_callback_function( $args ){
//The markup for your meta box goes here
}
I just created a custom post type called 'mtl_chapter'. With my script, I assign a post parent of my CPT to the regular 'post' type. So, my CPT are basically the children of my regular posts. I want to change the permalink structure of my CPT from
/base-slug/cpt-post-title/
to
/parent-title/cpt-post-title/
So, It will look like the attachment posts do with the permalink:
/parent-title/attachment-post-title/
My current code is able to change the permalink structure to what I want, but I get
404 not found
when I click the link. Please help me, here is my current code:
function create_posttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => '%parent-post-name%','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_posttype' );
add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);
function mtl_update_permalink_structure( $post_link, $post )
{
if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
$parent_id = wp_get_post_parent_id($post->ID);
$parent_post = get_post($parent_id);
$slug = $parent_post->post_name;
if ( $slug ) {
$post_link = str_replace( '%parent-post-name%', $slug, $post_link );
}
}
return $post_link;
}
try using
function create_myposttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'hierarchical' => true,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => 'mtl_chapter','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5,
'supports' => array(
'page-attributes' /* This will show the post parent field */,
'title',
'editor',
),
)
);
}
add_action( 'init', 'create_myposttype' );
i am create a wordpress plugin, this plugin add custompost but this post not see featured image
add_theme_support('post-thumbnails',array('km-lightbox','post'));
add_action( 'init', 'create_posttype_light_box' );
function create_posttype_light_box() {
register_post_type( 'km-lightbox',
array(
'labels' => array(
'name' => __( 'Lightboxs' ),
'singular_name' => __( 'lightbox' )
),
'public' => true,
'supports'=> array('title', 'editor','thumbnail' ),
'has_archive' => true,
'rewrite' => array('slug' => 'lightbox')
)
);
}