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!
Related
What is the best way to structure a parent/child custom post type combo, so that my slug url can look like this
https://www.mysite.co.uk/system/parent/child
The system part of the slug is vital, it is part of the multiple theme plugin for wordpress. Also, the parent/child parts of the url will not always be the same. Lets say I make a course called "How to ride a bike", and a series inside it called "Series 1", my slug needs to look like this
https://www.mysite.co.uk/system/how-to-ride-a-bike/series-1
at the moment my custom post types look like this
<?php
function create_post_type() {
//Courses
register_post_type( 'courses',
[
'labels' => array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' )
),
'description' => 'All courses',
'public' => true,
'rewrite' => array(
'slug' => 'system',
),
'menu_icon' => 'dashicons-welcome-learn-more',
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
//Series
register_post_type( 'series',
[
'labels' => array(
'name' => __( 'Series' ),
'singular_name' => __( 'Series' )
),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type=courses',
'description' => 'Course series',
'public' => true,
'rewrite' => array('slug' => '/'),
'hierarchical' => true,
'with_front' => false,
'capability_type' => 'post',
'has_archive' => false,
'supports' => ['title', 'custom_fields', 'page-attributes']
]
);
}
add_action( 'init', 'create_post_type' );
?>
I'm not 100% sure on the best way to actually link the child element to the parent? At the moment I have an advanced custom field on the child post type (series) where you select the parent (course) its linked to.
What is the best way to structure my custom post types, so that I can link a child to a parent and have the slug specified above?
You can use parent/child structures of the same custom post type using the page-attributes value in the supports array.
You do need to flush the rewrite rules of Wordpress before adding the sub-item. Just visit the permalinks page and hit save a couple of times.
Here is my example with the custom post type subcase:
register_post_type( 'subcase',
array(
'hierarchical' => true, // needs to be true
'labels' => array(
'name' => __( 'Sub Cases' ),
'singular_name' => __( 'Sub Case' )
),
'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'has_archive' => false,
'rewrite' => array(
'slug' => 'cases/subcases',
'with_front' => true
),
'supports' => array(
'page-attributes', // creates parent select box
'title',
'editor',
'excerpt',
'thumbnail'
)
)
);
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
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')
)
);
}
I have created 2 custom post types. One called artist and one called work. When I create a post in artist and view it the link is fine and I can view the content. However in work when I add a new post and go to view it it says that it cannot find the content and the page is missing.
Here is my functions.php code.
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'artist',
array(
'labels' => array(
'name' => __( 'Talent' ),
'singular_name' => __( 'talent' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'talent'),
)
);
register_post_type( 'work',
array(
'labels' => array(
'name' => __( 'Content' ),
'singular_name' => __( 'content' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'thumbnail' ),
'supports' => array('thumbnail', 'title', 'editor'),
'rewrite' => array('slug' => 'content'),
)
);
}
I've got some Wordpress CPTs. They correct URL should be /wordpress/training/training-page/, and that's the URL I see in the admin manage page, but when I click the link the URL I end up with is /wordpress/blog/2010/05/21/training-page/.
I deactivated my plugins with no success. Does anyone know how to keep the correct URL intact?
Here's my code:
<?php
add_action( 'init', 'tv_content_posttype' );
function tv_content_posttype() {
register_taxonomy('content',
'training',
array(
'hierarchical' => true,
'label' => 'Content Index',
'query_var' => true,
'rewrite' => true
)
);
register_post_type( 'training',
array(
'type' => 'page',
'labels' => array(
'name' => __( 'TV Training' ),
'singular_name' => __( 'TV Training' )
),
'public' => true,
'rewrite' => array(
'with_front' => false,
'slug' => 'training',
),
'hierarchical' => true,
'query_var' => true,
'taxonomies' => array( 'content' ),
)
);
}
Just a few observations: there's no such thing as a 'type' argument for register_post_type(), so you can get rid of that line. Second, 'with_front' => false is telling WordPress that the URL structure should be /training/training-page/. /wordpress/ in this isntance is the 'front' part you're telling it to leave off. Additionally, you don't need to add 'taxonomies' for the post_type, but you do need to register the post type before you register the taxonomy. So try this:
<?php
add_action( 'init', 'tv_content_posttype' );
function tv_content_posttype() {
register_post_type( 'training',
array(
'labels' => array(
'name' => __( 'TV Training' ),
'singular_name' => __( 'TV Training' )
),
'public' => true,
'rewrite' => array(
'with_front' => true,
'slug' => 'training',
),
'hierarchical' => true,
'query_var' => true,
)
);
register_taxonomy('content',
'training',
array(
'hierarchical' => true,
'label' => 'Content Index',
'query_var' => true,
'rewrite' => true
)
);
}