I’ve been trying for days to create a custom post type, with categories. So far, I have this working, and I can add content easily, and assign it to a category. My code is below.
What I don’t understand, and can’t seem to work is creating an archive page to display posts of a category.
For example: My post type is called Adverts. My category is called Photographers.
Is it possible for the page to dynamically work out what category you’re on and display all custom posts belonging to that category?
function my_custom_post_advert() {
$labels = array(
'name' => _x( 'Adverts', 'post type general name' ),
'singular_name' => _x( 'Advert', 'post type singular name' ),
'add_new' => _x( 'Add New', 'advert' ),
'add_new_item' => __( 'Add New Advert' ),
'edit_item' => __( 'Edit Advert' ),
'new_item' => __( 'New Advert' ),
'all_items' => __( 'All Adverts' ),
'view_item' => __( 'View Advert' ),
'search_items' => __( 'Search Adverts' ),
'not_found' => __( 'No adverts found' ),
'not_found_in_trash' => __( 'No adverts found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Adverts'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our adverts and advert specific data',
'public' => true,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'category' ),
'has_archive' => true,
);
register_post_type( 'advert', $args );
}
add_action( 'init', 'my_custom_post_advert' );
function my_taxonomies_advert() {
$labels = array(
'name' => _x( 'Advert Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Advert Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Advert Categories' ),
'all_items' => __( 'All Advert Categories' ),
'parent_item' => __( 'Parent Advert Category' ),
'parent_item_colon' => __( 'Parent Advert Category:' ),
'edit_item' => __( 'Edit Advert Category' ),
'update_item' => __( 'Update Advert Category' ),
'add_new_item' => __( 'Add New Advert Category' ),
'new_item_name' => __( 'New Advert Category' ),
'menu_name' => __( 'Advert Categories' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
);
register_taxonomy( 'advert_category', 'advert', $args );
}
add_action( 'init', 'my_taxonomies_advert', 0 );
You basically need to create a page template that contains a customized wp_query so WordPress can determine what category you’re on.
Once you have your page template created then you can create a page in your WordPress admin … selecting the new page template as your template.
And if you want the category to be dynamic you could always setup your page template to accept a $_GET parameter to determine which category to display adverts from. Like so:
or
etc.
Here’s an example of what the page template may look like (course this will vary on what theme you’re using … this example was built to use the twentyfourteen theme):
You should be able to navigate to /adverts. Also,
has_archive
should create an archives page for you.To save yourself a lot of hassle and what I’ve used in the past is this custom post type plugin – it works like a charm:
And with that I use this Custom post type archive plugin:
The solution is essentially contained in the answer to this question elsewhere on StackOverflow.
To summarize, build a custom query, but in the $args array replace:
with a taxonomy query, like so:
Of course, you should include
'post-type' => 'advert'
in $args as well. Hope this helps!So I also had a need for custom post types with categories.
The code below is really simple and clean. Literally copy and paste. and then just adjust to your needs. Hope this helps people in the future.
It basically links the normal wordpress categories with your custom post types. making it really easy for your client when they are working from the wordpress admin section. It also has individual taxonomy via tags. so you have the option to have categories through all post types or post specific taxonomy.
The code is pretty self-explanatory. Please vote for my answer I need to build up my rep. Thank you.
You must copy the code into your functions.php file
There are 4 custom post types so like I said the code is pretty self-explanatory