最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Permalinks for a custom post type - with a page template (custom query) and taxonomy archive

programmeradmin5浏览0评论

I'm having trouble getting my permalinks working with a specific custom post type setup.

Here's the code for the custom post type:

function jc_attactions() {
$labels = array(
    'name' => 'Attractions',
    'singular_name' => 'Attraction',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Attraction',
    'edit_item' => 'Edit Attraction',
    'new_item' => 'New Attraction',
    'all_items' => 'All Attractions',
    'view_item' => 'View Attraction',
    'search_items' => 'Search Attractions',
    'not_found' => 'No Attractions found',
    'not_found_in_trash' => 'No Attractions found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Attractions'
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'menu_icon' => 'dashicons-tickets-alt',
    'show_ui' => true,
    'query_var' => true,        
    'rewrite' => array( 'slug' => 'attractions', 'with_front' => false  ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'has_archive' => false,
    'supports' => array('title', 'editor')
  );
register_post_type( 'attractions_cpt' , $args );

Instead of using the archive template, I use a page with a custom template/query to render the results. In this case, the template simply outputs the list of taxonomy terms with some stuff from ACF fields on the term etc.. This part is working just fine.

I also have a custom taxonomy. Code:

function create_attracts_tax() {
$labels = array(
    'name'              => 'Category',
    'singular_name'     => 'Category',
    'search_items'      => 'Search Categories',
    'all_items'         => 'All Categories',
    'parent_item'       => 'Parent Category',
    'parent_item_colon' => 'Parent Category:',
    'edit_item'         => 'Edit Category', 
    'update_item'       => 'Update Category',
    'add_new_item'      => 'Add New Category',
    'new_item_name'     => 'New Category',      
    'menu_name'         => 'Categories'
);
$args = array(
    'labels' => $labels,
    'show_admin_column' => true,
    'hierarchical' => true,
    'rewrite' => array( 'slug' => 'attractions', 'with_front' => false ),
    'show_ui' => true,          
    'query_var' => true,
    'public' => true        
);
register_taxonomy( 'attract_cats', 'attractions_cpt', $args );

There are no detail pages for these, I simply have a page, /attractions/ and a taxonomy (will use default taxonomy template file) that on the taxonomy archive, will just list out the posts.

My problem is with permalinks. I would ideally like the permalink to simply be /attractions/taxonomy-slug/ for the archive pages.. Unfortunately no matter what I do I get 404's. If I simply add something like attractions/category to the taxonomy rewrite, it of course works and ends up being /attractions/category/business/ but I really would rather not have the extra "category" in there.

I've been googling (and even have a few posts bookmarked here over the years), but everything I find seems to be about getting creative for the actual single post page slugs. In this case, I am just looking for the tax archive to not 404.

Any ideas?

I'm having trouble getting my permalinks working with a specific custom post type setup.

Here's the code for the custom post type:

function jc_attactions() {
$labels = array(
    'name' => 'Attractions',
    'singular_name' => 'Attraction',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Attraction',
    'edit_item' => 'Edit Attraction',
    'new_item' => 'New Attraction',
    'all_items' => 'All Attractions',
    'view_item' => 'View Attraction',
    'search_items' => 'Search Attractions',
    'not_found' => 'No Attractions found',
    'not_found_in_trash' => 'No Attractions found in Trash',
    'parent_item_colon' => '',
    'menu_name' => 'Attractions'
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'menu_icon' => 'dashicons-tickets-alt',
    'show_ui' => true,
    'query_var' => true,        
    'rewrite' => array( 'slug' => 'attractions', 'with_front' => false  ),
    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => null,
    'has_archive' => false,
    'supports' => array('title', 'editor')
  );
register_post_type( 'attractions_cpt' , $args );

Instead of using the archive template, I use a page with a custom template/query to render the results. In this case, the template simply outputs the list of taxonomy terms with some stuff from ACF fields on the term etc.. This part is working just fine.

I also have a custom taxonomy. Code:

function create_attracts_tax() {
$labels = array(
    'name'              => 'Category',
    'singular_name'     => 'Category',
    'search_items'      => 'Search Categories',
    'all_items'         => 'All Categories',
    'parent_item'       => 'Parent Category',
    'parent_item_colon' => 'Parent Category:',
    'edit_item'         => 'Edit Category', 
    'update_item'       => 'Update Category',
    'add_new_item'      => 'Add New Category',
    'new_item_name'     => 'New Category',      
    'menu_name'         => 'Categories'
);
$args = array(
    'labels' => $labels,
    'show_admin_column' => true,
    'hierarchical' => true,
    'rewrite' => array( 'slug' => 'attractions', 'with_front' => false ),
    'show_ui' => true,          
    'query_var' => true,
    'public' => true        
);
register_taxonomy( 'attract_cats', 'attractions_cpt', $args );

There are no detail pages for these, I simply have a page, /attractions/ and a taxonomy (will use default taxonomy template file) that on the taxonomy archive, will just list out the posts.

My problem is with permalinks. I would ideally like the permalink to simply be /attractions/taxonomy-slug/ for the archive pages.. Unfortunately no matter what I do I get 404's. If I simply add something like attractions/category to the taxonomy rewrite, it of course works and ends up being /attractions/category/business/ but I really would rather not have the extra "category" in there.

I've been googling (and even have a few posts bookmarked here over the years), but everything I find seems to be about getting creative for the actual single post page slugs. In this case, I am just looking for the tax archive to not 404.

Any ideas?

Share Improve this question edited Jun 7, 2016 at 13:30 Tim Malone 4,8045 gold badges29 silver badges41 bronze badges asked Jun 6, 2016 at 19:20 61Pixels61Pixels 1364 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I've actually had a brain fart, and just figured it out.. Since I don't have any single posts, there was no reason to even be rewriting the CPT itself. I simply removed the rewrite line from the CPT and everything is working perfectly.

发布评论

评论列表(0)

  1. 暂无评论