2014-05-20 4 views
0

I은 ​​ID가 제품의 사후 ID, 예를 들면이다Woocommerce 제품 ID는

domain.com/product-category/id/product-title 

의 URL에 Woocommerce 제품 ID를 넣어하는 방법, 예컨대 : 찾고 있어요 "12092". WooCommerce에 의해 자동으로 생성 된 Wordpress.

방법으로 수행 할 수 있습니까? Wordpress permalinks 설정을 통해 쉽게 또는 파일을 해킹하여 다른 방법으로 쉽게.

<?php 

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3); 

function wpse33551_post_type_link($link, $post = 0){ 
if ($post->post_type == 'product'){ 
    return home_url('p/' . $post->ID); 
} else { 
    return $link; 
} 
} 

add_action('init', 'wpse33551_rewrites_init'); 

function wpse33551_rewrites_init(){ 
add_rewrite_rule(
    'product/([0-9]+)?$', 
    'index.php?post_type=product&p=$matches[1]', 
    'top'); 
} 

?> 

을하지만 출력은 다음과 같습니다 :

이 거의 작동 나는 그것이 싶습니다

domain.com/p/45 

:

domain.com/p/45/title-of-the-post 

감사합니다!

+0

다음 링크는 도움이 될 수 있습니다. http://wordpress.stackexchange.com/questions/33551/how-to-rewrite-uri-of-custom-post-type/33555#33555 –

+0

거의 없습니다. 이제 표시됩니다 : http://example.com/product/142 하지만 아래에 나와 있습니다 (/ p는/제품 대신 내 슬러그 임) http://example.com/ p/142/* 게시물 제목 또는 제품 번호 * 위의 제안에서 코드를 변경해야하는 사항은 무엇입니까? – simonvk

+0

게시물을 지금까지 작성한 코드로 업데이트하면 다른 사람이 도움이됩니다. –

답변

0

$post->post_name을 사용하면 슬러그를 얻을 수 있습니다.

/p/[id][anything]

<?php 

add_filter('post_type_link', 'wpse33551_post_type_link', 1, 3); 
function wpse33551_post_type_link($link, $post = 0){ 
    if ($post->post_type != 'product') 
     return $link; 

    return home_url('p/' . $post->ID . '/' . $post->post_name); 
} 

add_action('init', 'wpse33551_rewrites_init'); 
function wpse33551_rewrites_init(){ 
    add_rewrite_rule(
     'p/([0-9]+)?.*?$', 
     'index.php?post_type=product&p=$matches[1]', 
     'top' 
    ); 
} 

?> 
당신은 워드 프레스는 새 규칙을 받아들이 설정 메뉴에 영구 링크를 '리셋'해야 할 수도 있습니다

로 끝나는 모든 URL을 받아 더 허용 정규 표현식을 사용합니다.