2016-09-27 2 views
0

the_permalink() 기능 등의 출력을 변경하고 싶습니다. 따라서 모든 포스트 유형, '포스트'포스트 유형 및 단일 페이지에 대한 멀 마린 크 구조를 변경하고 싶지는 않습니다.단일 포스트 permalink의 기본 구조 생성을 변경하는 방법은 무엇입니까?

mysite.com/news/2016/the-news-title 

제대로 한 후 페이지 템플릿 및 표시 내용을 사용자에게 드라이브 것 있도록 나는 이미 재 작성 규칙을 변경. 벌금. 지금, 내가 말했듯이, the_permalink() 함수는 그 구조를 가진 링크를 생성하기를 원합니다.

어떻게하면됩니까?

답변

0

다음은 Noman 표시입니다. 제 대답을 게시 할 수 있습니다. 'the_permalink'필터를 사용하여 post_type == 'post'인 경우 출력을 수정할 수 있습니다. 기본 코드 :

add_filter('the_permalink', 'edit_the_permalink', 10, 2); 
function edit_the_permalink($permalink, $post) { 
    // you should pass the post object when calling the_permalink() 
    if(is_object($post) && $post->post_type == 'post'){ 
    // [...do the magic...] 
    return $permalink; 
    } 
} 

모두 the_permalink()get_permalink() (별칭 get_the_permalink())은 $ 포스트 인수에 동의 함을 유의하시기 바랍니다 당신은 포스트 객체로 전달한다.

또한 맞춤 게시물 유형 퍼멀 링크 생성 (출력 : get_post_permalink())을 수정하려는 경우 post_type_link 필터로 거의 동일하게 처리 할 수 ​​있습니다. 이 경우 콜백은 $ post_link, $ post, $ leavename, $ sample의 4 가지 매개 변수를받습니다.

0

나는 적절한 해결책을 찾을 수 없습니다하지만 당신은 .. 왜 get_post() 때문에 post_name (고유 주소)를 사용하여이 분야에 저장된 객체 WP_Post $postpost 당신은 포스트 유형 위해 귀하의 고유 주소를 생성 할 수 있으며 항상 것입니다 독특한.

function edit_the_permalink($permalink) { 
    $post = get_post(array('post_name' => $permalink)); 
    if($post->post_type == 'post') { 
     // Override your permalink here for post_type = `post` 
     echo '<pre>';print_r($post);echo '</pre>'; 
     $permalink = ''; 
     return $permalink; 
    } 
} 
add_filter('the_permalink', 'edit_the_permalink', 10, 1); 
+0

감사합니다.하지만 정말로 확신합니까? post_name은 슬러그에 사용됩니다 (실제로는 슬러그입니다). 퍼머 링크는 슬러그가 마지막 부분 일 때 더 많은 조각으로 구성됩니다. – Stratboy

+0

또한 the_permalink 필터는 2 개의 매개 변수를 받아들입니다. 두 번째 매개 변수는 게시물이므로 get_post를 사용할 필요가 없습니다. 대답을 수정 한 후 upvote 수 있습니다.) 어쨌든,이 필터는 내가 원하는 것을 적합합니다, 난 그냥 제대로 permalink 빌드 할 수 있습니다. 고맙습니다. – Stratboy

+0

@Stratboy 여기에 게시하기 전에 코드를 테스트했습니다. 내 경우에는 두 번째 매개 변수가'0'을 반환하고있는 이유는 permalink에'get_post'를 사용했기 때문입니다. 예 ** the permalink는 더 많은 조각으로 구성된 **이며 post_name은 항상 고유하므로 get_post 사용에 대해서는 걱정하지 마십시오. 나는 당신이 길을 찾을 수 있도록 도와 줘서 다행이다. :) 고마워요 :) – Noman