maksym1991
@maksym1991
WordPress adept

Пользовательская структура постоянных ссылок /%custom-taxonomy%/%post-name%/ (без CPT slug)?

Я пытаюсь создать собственную структуру постоянных ссылок, которая позволит мне достичь следующее:

У меня есть пользовательский тип записи "products"
У меня есть таксономия "catalogs" добавлена к CPT "products"
Я хочу, чтобы моя структура постоянных ссылок выглядела так:

catalogs/product-name - без CPT слага или

%custom-taxonomy%/%post-name%/

Также нужна работающая страница архивов(/products).

Мне удалось добиться такой структуры(/%post-name%):

function remove_cpt_slug( $post_link, $post ) {
    if ( 'products' === $post->post_type && 'publish' === $post->post_status ) {
        $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
    }

    return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );

function parse_request ( $query ) {
    if ( ! $query->is_main_query() )
        return;

    if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
        return;
    }

    if ( ! empty( $query->query['name'] ) ) {
        $query->set( 'post_type', array( 'post', 'page', 'products' ) );
    }
}
add_action( 'pre_get_posts', 'parse_request' );


Буду очень благодарен за помощь
  • Вопрос задан
  • 509 просмотров
Решения вопроса 1
maksym1991
@maksym1991 Автор вопроса
WordPress adept
Удалось найти решение:
$args   = array(
    		'labels'             => $labels,
    		'public'             => true,
    		'publicly_queryable' => true,
    		'show_ui'            => true,
    		'show_in_menu'       => true,
    		'query_var'          => true,
    		'rewrite'            => array ( 'slug' => 'products/%catalogs%', 'with_front' => false),
    		'capability_type'    => 'post',
    		'has_archive'        => 'products',
    		'hierarchical'       => false,
    		'menu_position'      => 4,
    		'menu_icon'          => 'dashicons-cart',
    		'supports'           => array( 'title', 'thumbnail' ),
    		'taxonomies'         => array( 'catalogs' ),
    	);
    register_post_type( 'products', $args );
    
    register_taxonomy( 'catalogs', 'products', array(
    		'hierarchical'      => true,
    		'labels'            => $labels,
    		'show_ui'           => true,
    		'query_var'         => true,
    		'show_admin_column' => true,
    		'show_tagcloud'     => false,
    		'rewrite'           => array( 'slug' => 'products', 'with_front' => false ),
    	) );
    
    /** 
Remove taxony slug, remove CPT slug and add new rewrite rule 
*/
function remove_tax_slug_link( $link, $term, $taxonomy ) {
    if ( $taxonomy !== 'catalogs' )
        return $link;
 
    return str_replace( 'products/', '', $link );
}
add_filter( 'term_link', 'remove_tax_slug_link', 10, 3 );
 
function custom_tax_rewrite_rule() {
	$cats = get_terms(
			'catalogs', array(
			'hide_empty' => false,
		)
	);
	if( sizeof( $cats ) )
		foreach($cats as $cat)
			add_rewrite_rule( $cat->slug.'/?$', 'index.php?catalogs='.$cat->slug, 'top' );
}
add_action('init', 'custom_tax_rewrite_rule');

function change_permalinks( $post_link, $post ) {
    if ( is_object( $post ) && $post->post_type == 'products' ){
    	$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
        $terms = wp_get_object_terms( $post->ID, 'catalogs' );
        if( $terms ) {
            return str_replace( '%catalogs%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'change_permalinks', 10, 2 );

function entry_rewrite_rules() {
    $custom_structure = '/%catalogs%/%products%';
    add_rewrite_tag( '%products%', '([^/]+)', 'products=' );
    add_permastruct( 'products', $custom_structure, array( 'walk_dirs' => false ) );

}
add_action( 'init', 'entry_rewrite_rules' );
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы