I have a custom post type named Sectors, which was made using the code below:
add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {
register_post_type( 'sectors',
array(
'labels' => array(
'name' => __( 'Sectors' ),
'singular_name' => __( 'sector' ),
),
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-heart',
'public' => true,
'rewrite' => array( 'slug' => 'sectors', 'with_front' => false ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'sectors', 'post_tag' ),
));
}
Then I have this code, which adds the categories to sectors:
add_action( 'init', 'create_sector_cat_categories' );
function create_sector_cat_categories() {
register_taxonomy(
'SectorCategories',
'sectors',
array(
'label' => __( 'Sector Categories' ),
'rewrite' => array( 'slug' => 'sectorcategories' ),
'hierarchical' => true,
)
);
}
So, when I go to my sectors page, I currently have a list of the categories using the page 'sectorcategories.php'.
Then when you click on a sector category, this goes to display all sectors in this category, using taxonomy-sectorcategories.php.
Now, the issue I have, is when I click onto a sector from here, It is using the index.php file, yet I have a page setup for this called single-sectors.php.
Does anyone know why it is not following its taxonomy?
Sometimes if you make taxonomies on the fly, wordpress does't update it. Just goto your settings > Permalinks and click update
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'
)
)
);
I have set a Custom Post Type using the following code:
add_action( 'init', 'wpshout_register_major' );
function wpshout_register_major() {
$args = array(
'public' => true,
'label' => 'Major',
'has_archive' => true,
'rewrite' => array( 'slug' => '%category%' ),
'supports' => array(
'title',
'editor',
'author',
'thumbnail',
'excerpt',
'comments',
'revisions'
),
'taxonomies' => array( 'category' )
);
register_post_type( 'major', $args );
}
Everything works fine, except for the page loading a 404 page.
When the following line is changed to a fixed string, then it works and loads successfully:
'rewrite' => array( 'slug' => 'major' ),
But i don't want fixed string in the URL, i want category which is link to the custom post.
Could you please help with this?
Thanks in advance.
I am trying to create a Custom post type in Wordpress and I'm actually stuck at this , because its not working and I don't know why.
I copied the Custom post type code from here: https://codex.wordpress.org/Post_Types#Custom_Post_Type_Templates
And this is the code i added in my functions.php
add_action( 'init', 'create_posttype' );
function create_posttype() {
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'capability_type' => 'post',
'hierarchical' => false,
'supports' => array('title', 'editor', 'thumbnail', 'comments', 'page-attributes'),
'has_archive' => true,
'rewrite' => array('slug' => 'products'),
)
);
}
and then i used this code below to create a new post:
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $my_title ),
'post_content' => $my_content,
'post_status' => 'inherit',
'post_author' => 1,
'post_type' => 'product',
);
// Insert the post into the database
wp_insert_post( $my_post );
Then i tried to access it the link in the url, but i got a 404 page not found error !
Please any one know what i'm doing wrong?
Try this
function ProductPostType() {
register_post_type( 'product',
array(
'labels' => array(
'name' => __( 'Product' ),
'singular_name' => __( 'product' )
),
'public' => true,
'has_archive' => 'themes',
'rewrite' => array('slug' => 'product'),
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt'),
'description' => 'Custom Theme Posts',
'show_ui' => true,
'show_in_menu' => true,
)
);
}
add_action( 'init', 'ProductPostType' );
Just paste this code in function.php
Here is a super simple trick that may or may not work for you.
In the WP-Backend, locate Settings and click on the Permalinks menu.
On the Permalinks Settings Page, simply choose Plain... and save the Changes.
Go to the Front-End and reload your Plugin to see if your Plugin now functions but without SEO-Friendly URL.
Whether it works or not; just go back to the Permalinks Settings & change back to Day and name. Save and try it out again....
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'),
)
);
}
How to create custom post types in wordpress with its category
I want to create a news section in my site (same as posts), but i want to create my own news section having post type as news. Please make suggestion.
try this :
<?php
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'news',
array(
'labels' => array(
'name' => __( 'News' ),
'singular_name' => __( 'News' )
),
'capability_type' => 'post',
'public' => true,
'supports' => array(
'title',
'editor',
'excerpt',
'trackbacks',
'custom-fields',
'revisions',
'thumbnail',
'author',
'page-attributes',
)
)
);
}
register_taxonomy( 'news_category', 'news',array('label' => __( 'Categories' ),'rewrite' => array( 'slug' => 'news_category' ),'hierarchical' => true, ) );
?>