2013-04-05 2 views
1

이 자체 Q 워드 프레스 다음 상위 페이지 링크

가 종종 워드 프레스 당신이 프로젝트 구조를 구축하기 위해 페이지 계층 구조를 사용 & A.입니다. 포트폴리오 사이트를 수행 할 때 예를 들어,이 일반적입니다 :

    • BMW
      • 모델
        • 3 시리즈
        • 5 시리즈
    • 아우디사용자가 3 시리즈 페이지에있을 때개
      • 모델
        • A3
        • A4
      그래서

, 종종 당신은이 싶습니다 "다음 자동차 제조업체"링크를 클릭하십시오. 플러그인없이 어떻게 할 수 있습니까?

답변

2

이 기능을 사용하여 다음 페이지를 결정하는 데 사용할 심도를 설정할 수 있습니다. 그래서 질문에서, 사용자는 '3 시리즈'에 있었기 때문에 깊이는 2가 될 것입니다. 따라서 링크는 "Audi"페이지로 되돌아갑니다.

$nextIMG = '<img src="'.get_template_directory_uri().'/images/icon-nav-right.png"/>';    
echo next_project_link($nextIMG); 

을 그리고 functions.php이 장소 :

그것은 당신의 템플릿에 다음과 같이 사용되는 (내 예는 링크 텍스트에 대한 이미지를 사용)

/* 
* Next Project Link 
*/ 
    function next_project_link($html) { 
     global $post; 

     // Change this to set what depth you want the next page of 
     $parent_depth = 2; 

     $ancestors = get_post_ancestors($post);  
     $current_project_id = $ancestors[$parent_depth-1]; 

     // Check for cached $pages 
     $pages = get_transient('all_pages'); 
     if (empty($transient)){ 
      $args = array(
       'post_type'   => 'page', 
       'order'    => 'ASC', 
       'orderby'   => 'menu_order', 
       'post_parent'  => $ancestors[$parent_depth], 
       'fields'   => 'ids', 
       'posts_per_page' => -1 
      ); 
      $pages = get_posts($args); 
      set_transient('all_pages', $pages, 10); 
     }  

     $current_key = array_search($current_project_id, $pages); 
     $next_page_id = $pages[$current_key+1]; 

     if(isset($next_page_id)) { 
      // Next page exists 
      return '<a class="next-project" href="'.get_permalink($next_page_id).'">'.$html.'</a>'; 
     } 

    } 


/* 
* Previous Project Link 
*/ 
    function previous_project_link($html) { 
     global $post; 

     // Change this to set what depth you want the next page of 
     $parent_depth = 2; 

     $ancestors = get_post_ancestors($post); 
     $current_project_id = $ancestors[$parent_depth-1]; 

     // Check for cached $pages 
     $pages = get_transient('all_pages'); 
     if (empty($transient)){ 
      $args = array(
       'post_type'   => 'page', 
       'order'    => 'ASC', 
       'orderby'   => 'menu_order', 
       'post_parent'  => $ancestors[$parent_depth], 
       'fields'   => 'ids', 
       'posts_per_page' => -1 
      ); 
      $pages = get_posts($args); 
      set_transient('all_pages', $pages, 10); 
     }  

     $current_key = array_search($current_project_id, $pages); 
     $prev_page_id = $pages[$current_key-1]; 

     if(isset($prev_page_id)) { 
      // Previous page exists 
      return '<a class="previous-project" href="'.get_permalink($prev_page_id).'">'.$html.'</a>'; 
     } 

    } 
+0

당신이 당신의 질문에 대답 않았다 누군가 응답하기도 전에? –

+0

S.O가 가진 Q & A 기능을 사용했습니다. –

+1

@Draew Baker. 이것은 좋은 코드이지만 페이지가로드 될 때마다 쿼리를 저장하기 위해 [일시적인 API] (http://codex.wordpress.org/Transients_API)에 쿼리를 추가합니다. 특히 카테고리/CPT/페이지가 자주 변경되지 않는 경우 특히 그렇습니다. –