admin管理员组文章数量:1122832
I have a custom post type functionality defined on my website for a certain action. In order to achieve a specific type of url structure. I removed the constant slug from it and replaced it with category and brand name.
The previous url was
www.website/product/name
The new url is
www.website/category/brand/name
You see, I have totally removed the slug. Here is the code for that. Now the issue is that these urls are not working and I am geting a 404 page in return. I have tried flushing the permailinks as well but it is not helping.
According to my understanding because of removing the slug, the query is searching in 'Post' not the 'Custom Post Type' but I don't know how to correct it. You response will be highly appreciated.
// Register our Custom Post type as aps-products
public static function register_cpt_aps_products() {
$permalinks = get_aps_settings('permalinks');
$slug = (isset($permalinks['product-slug'])) ? $permalinks['product-slug'] : '';
// labels text for our post type aps-products
$labels = array(
// post type general name
'name' => __( 'APS Products', 'aps-text' ),
// post type singular name
'singular_name' => __( 'APS Product', 'aps-text' ),
'name_admin_bar' => __( 'APS Product', 'aps-text' ),
'menu_name' => __( 'APS Products', 'aps-text' ),
'add_new' => __( 'Add New APS Product', 'aps-text' ),
'add_new_item' => __( 'Add New APS Product', 'aps-text' ),
'edit_item' => __( 'Edit APS Product', 'aps-text' ),
'new_item' => __( 'New APS Product', 'aps-text' ),
'view_item' => __( 'View APS Product', 'aps-text' ),
'archives' => __( 'APS Products Archives', 'aps-text' ),
'search_items' => __( 'Search APS Products', 'aps-text' ),
'insert_into_item' => __( 'Insert into APS Product', 'aps-text' ),
'featured_image' => __( 'APS Product Image', 'aps-text' ),
'set_featured_image' => __( 'Set APS Product Image', 'aps-text' ),
'remove_featured_image' => __( 'Remove APS Product Image', 'aps-text' ),
'use_featured_image' => __( 'Use as APS Product image', 'aps-text' ),
'not_found' => __( 'No APS Products found', 'aps-text' ),
'not_found_in_trash' => __( 'No APS Products found in Trash', 'aps-text' )
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'query_var' => true,
'publicly_queryable' => true,
'show_in_nav_menus' => false,
'menu_icon' => 'dashicons-products',
'capability_type' => 'aps-products',
'capabilities' => array(
'read_post' => 'read_aps_product',
'edit_post' => 'edit_aps_product',
'edit_posts' => 'edit_aps_products',
'delete_posts' => 'delete_aps_products',
'create_posts' => 'create_aps_products',
'publish_posts' => 'publish_aps_products',
'edit_published_posts' => 'edit_published_aps_products',
'delete_published_posts' => 'delete_published_aps_products',
'edit_others_posts' => 'edit_others_aps_products',
'delete_others_posts' => 'delete_others_aps_products',
'read_private_posts' => 'read_private_aps_products',
'edit_private_posts' => 'edit_private_aps_products',
'delete_private_posts' => 'delete_private_aps_products'
),
'map_meta_cap' => true,
'hierarchical' => true,
'taxonomies' => array('post_tag','aps-cats', 'aps-brands', 'aps-attributes', 'aps-filters', 'aps-rating-bars'),
'has_archive' => true,
'show_in_menu' => 'aps-products',
'supports' => array( 'publicize','title', 'editor', 'thumbnail', 'comments', 'author', 'excerpt' ),
'register_meta_box_cb' => array(__CLASS__, 'add_aps_products_metabox'),
'rewrite' => array('slug' => '/%aps-cats%/%aps-brands%/', 'with_front' => false)
);
I have a custom post type functionality defined on my website for a certain action. In order to achieve a specific type of url structure. I removed the constant slug from it and replaced it with category and brand name.
The previous url was
www.website.com/product/name
The new url is
www.website.com/category/brand/name
You see, I have totally removed the slug. Here is the code for that. Now the issue is that these urls are not working and I am geting a 404 page in return. I have tried flushing the permailinks as well but it is not helping.
According to my understanding because of removing the slug, the query is searching in 'Post' not the 'Custom Post Type' but I don't know how to correct it. You response will be highly appreciated.
// Register our Custom Post type as aps-products
public static function register_cpt_aps_products() {
$permalinks = get_aps_settings('permalinks');
$slug = (isset($permalinks['product-slug'])) ? $permalinks['product-slug'] : '';
// labels text for our post type aps-products
$labels = array(
// post type general name
'name' => __( 'APS Products', 'aps-text' ),
// post type singular name
'singular_name' => __( 'APS Product', 'aps-text' ),
'name_admin_bar' => __( 'APS Product', 'aps-text' ),
'menu_name' => __( 'APS Products', 'aps-text' ),
'add_new' => __( 'Add New APS Product', 'aps-text' ),
'add_new_item' => __( 'Add New APS Product', 'aps-text' ),
'edit_item' => __( 'Edit APS Product', 'aps-text' ),
'new_item' => __( 'New APS Product', 'aps-text' ),
'view_item' => __( 'View APS Product', 'aps-text' ),
'archives' => __( 'APS Products Archives', 'aps-text' ),
'search_items' => __( 'Search APS Products', 'aps-text' ),
'insert_into_item' => __( 'Insert into APS Product', 'aps-text' ),
'featured_image' => __( 'APS Product Image', 'aps-text' ),
'set_featured_image' => __( 'Set APS Product Image', 'aps-text' ),
'remove_featured_image' => __( 'Remove APS Product Image', 'aps-text' ),
'use_featured_image' => __( 'Use as APS Product image', 'aps-text' ),
'not_found' => __( 'No APS Products found', 'aps-text' ),
'not_found_in_trash' => __( 'No APS Products found in Trash', 'aps-text' )
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'query_var' => true,
'publicly_queryable' => true,
'show_in_nav_menus' => false,
'menu_icon' => 'dashicons-products',
'capability_type' => 'aps-products',
'capabilities' => array(
'read_post' => 'read_aps_product',
'edit_post' => 'edit_aps_product',
'edit_posts' => 'edit_aps_products',
'delete_posts' => 'delete_aps_products',
'create_posts' => 'create_aps_products',
'publish_posts' => 'publish_aps_products',
'edit_published_posts' => 'edit_published_aps_products',
'delete_published_posts' => 'delete_published_aps_products',
'edit_others_posts' => 'edit_others_aps_products',
'delete_others_posts' => 'delete_others_aps_products',
'read_private_posts' => 'read_private_aps_products',
'edit_private_posts' => 'edit_private_aps_products',
'delete_private_posts' => 'delete_private_aps_products'
),
'map_meta_cap' => true,
'hierarchical' => true,
'taxonomies' => array('post_tag','aps-cats', 'aps-brands', 'aps-attributes', 'aps-filters', 'aps-rating-bars'),
'has_archive' => true,
'show_in_menu' => 'aps-products',
'supports' => array( 'publicize','title', 'editor', 'thumbnail', 'comments', 'author', 'excerpt' ),
'register_meta_box_cb' => array(__CLASS__, 'add_aps_products_metabox'),
'rewrite' => array('slug' => '/%aps-cats%/%aps-brands%/', 'with_front' => false)
);
Share
Improve this question
edited Sep 1, 2017 at 8:30
fuxia♦
107k38 gold badges255 silver badges459 bronze badges
asked Sep 1, 2017 at 8:18
RookieRookie
1231 gold badge1 silver badge8 bronze badges
2
- 1 Didn't look into your code bc no time, but did you save your permalinks after changing the functions.php? Everytime you change a permalink structure (like the one of your CPT) you have to go to "Settings" -> "Permalinks" and click save. – HU is Sebastian Commented Sep 1, 2017 at 8:49
- Yes. I did save. Here is the screenshot of request and how it is only searching in 'post'. prntscr.com/gfrm31 Brand and Category are already APS taxonomies and defined. – Rookie Commented Sep 1, 2017 at 10:59
2 Answers
Reset to default 0Go to setting > permalinks and save it, then open www.website.com/category/brand/name
Double check your rewrite slug. you can try somthing like this.
add_action('init', 'create_brand');
function create_brand()
{
register_taxonomy
(
'sumo-brand-letter',
array(),
array
(
'hierarchical' => true,
'labels' => array
(
'name' => _x('Letters', 'taxonomy general name'),
'singular_name' => _x('Letter', 'taxonomy singular name')
# And so one
),
'show_ui' => true,
'query_var' => 'brand-letter',
'rewrite' => array(
'slug' => 'brand',
),
)
);
register_post_type
(
'sumo-brand-term',
array
(
'labels' => array
(
'name' => _x('Brand Terms', 'post type general name'),
'singular_name' => _x('Brand Term', 'post type singular name')
# And so on …
),
'supports' => array('title', 'editor', 'thumbnail'),
'public' => true,
'rewrite' => array
(
'slug' => 'brand/%sumo-brand-letter%',
'with_front' => false
),
'query_var' => 'brand-term',
'has_archive' => 'brand',
'taxonomies' => array( 'sumo-brand-letter' ),
)
);
}
add_filter('post_type_link', 'brand_term_permalink', 10, 4);
function brand_term_permalink($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%sumo-brand-letter%' ) ) {
$brand_letter = get_the_terms( $post->ID, 'sumo-brand-letter' );
$post_link = str_replace( '%sumo-brand-letter%', array_pop( $brand_letter )->slug, $post_link );
}
return $post_link;
}
本文标签: plugin developmentCustom Post Type urls not working
版权声明:本文标题:plugin development - Custom Post Type urls not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288041a1928011.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论