이 기능을 사용하여 다음 페이지를 결정하는 데 사용할 심도를 설정할 수 있습니다. 그래서 질문에서, 사용자는 '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>';
}
}
당신이 당신의 질문에 대답 않았다 누군가 응답하기도 전에? –
S.O가 가진 Q & A 기능을 사용했습니다. –
@Draew Baker. 이것은 좋은 코드이지만 페이지가로드 될 때마다 쿼리를 저장하기 위해 [일시적인 API] (http://codex.wordpress.org/Transients_API)에 쿼리를 추가합니다. 특히 카테고리/CPT/페이지가 자주 변경되지 않는 경우 특히 그렇습니다. –