У меня проблема с добавлением второй таксономии в мою структуру постоянных ссылок cpt.
С одной Register_taxonomy (pxlr-products-category) все работает нормально и выглядит например: https://example.com/products/my_category/my_productname
Но мне нужен второй Register_taxonomy (pxlr-products-status). Когда я это делаю, я вижу в своей постоянной ссылке вместо налогового ярлыка только этот %status% https://example.com/products/my_categor ... roductname и получаю ошибку 404. Конечно, я удалил свои постоянные ссылки.
Во-вторых, (pxlr-products-status) может иметь значение типа (прекращено), но это не так. обязательно, оно также может быть пустым.
Мои постоянные ссылки должны выглядеть так https://example.com/products/my_categor ... roductname
Или посмотрите так https://example.com/products/my_category/my_productname
Вот мой CPT, я не могу найти свою ошибку и мне нужна помощь, спасибо :-/
class PXLR_Products
{
function __construct(){
// register post type
add_action('init', array($this, 'register_products_post_type'));
// register taxonomy
add_action('init', array($this, 'register_products_taxonomy'));
// slug - category filter
add_filter('post_type_link', [$this,'products_post_link'], 1, 3 );
// Rewrite support
add_action('init', [$this, 'custom_rewrite_rules']);
}
function custom_rewrite_rules() {
add_rewrite_rule(
'products/(.+?)/?$',
'index.php?pxlr-products-category=$matches[1]',
'bottom');
// add_rewrite_rule(
// 'products/(.+?)/(.+?)/?$',
// 'index.php?pxlr-products-category=$matches[1]&pxlr-products-status=$matches[2]',
// 'bottom');
}
public function register_products_post_type()
{
$labels = array(
'name' => _x('Products', 'Post Type General Name', 'pxlr-products'),
'singular_name' => _x('Products', 'Post Type Singular Name', 'pxlr-products'),
'menu_name' => __('PXLR Products', 'pxlr-products'),
'all_items' => __('All Products', 'pxlr-products'),
'view_item' => __('Show product', 'pxlr-products'),
'add_new_item' => __('Add product', 'pxlr-products'),
'add_new' => __('Add Product', 'pxlr-products'),
'edit_item' => __('Edit product', 'pxlr-products'),
'update_item' => __('Update product', 'pxlr-products'),
'search_items' => __('Search products', 'pxlr-products'),
'not_found' => __('No products found', 'pxlr-products'),
'not_found_in_trash' => __('No products found in trash.', 'pxlr-products'),
);
$args = array(
'labels' => $labels,
'name' => 'Products',
'name_admin_bar' => 'Products',
'singular_name' => 'Products',
'menu_icon' => 'dashicons-microphone',
'label' => __('Products', 'pxlr-products'),
'hierarchical' => true,
'show_in_menu' => true,
'show_in_rest' => true,
'show_in_nav_menus' => false,
'menu_position' => 28,
'supports' => array('title', 'editor', 'revisions', 'thumbnail', 'page-attributes', 'excerpt', 'custom-fields'),
'rest_base' => 'products',
'public' => true,
'show_ui' => true,
'has_archive' => 'products',
'can_export' => false,
'taxonomies' => array('pxlr-products-category', 'pxlr-products-status'),
'publicaly_queryable' => true,
'query_var' => true,
'exclude_from_search' => false,
//'rewrite' => ['slug' => _x('products/%category%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for one taxonomy if working fine!!!!
'rewrite' => ['slug' => _x('products/%category%/%status%', 'Post Type Slug', 'pxlr-products'), 'with_front' => true ], // for two taxonomies
'template_lock' => 'all',
);
register_post_type('pxlr-products', $args);
}
public function products_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-category' );
if( $terms ){
return str_replace( '%category%' , $terms[0]->slug , $post_link );
}
$terms = wp_get_object_terms( $post->ID, 'pxlr-products-status' );
if( $terms ){
return str_replace( '%status%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
public function register_products_taxonomy()
{
register_taxonomy(
'pxlr-products-category',
'pxlr-products',
array(
'label' => __('Categories Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products', 'Category Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
register_taxonomy(
'pxlr-products-status',
'pxlr-products',
array(
'label' => __('Status Product', 'pxlr-products'),
'hierarchical' => true,
'with_front' => false,
'show_in_rest' => true,
'rewrite'=> ['slug' => _x('products/%category%', 'Status Slug', 'pxlr-products'),
'with_front' => false,
'hierarchical' => true
],
'show_admin_column' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
)
);
}
}
Подробнее здесь: https://stackoverflow.com/questions/707 ... -structure
Как добавить 2 разные таксономии в структуру постоянных ссылок Wordpress CPT ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Показывать количество сообщений пользовательской таксономии по идентификатору таксономии
Anonymous » » в форуме Php - 0 Ответы
- 74 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Дочерний CPT WordPress отображает 404 после реализации пользовательских правил перезаписи
Anonymous » » в форуме Php - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-