這些函數允許您設置要用於確定下一頁的深度。所以在這個問題中,用戶是'3系列',所以深度是2.所以,返回的鏈接將是「奧迪」頁面。
它這樣使用在你的模板(我的例子是使用圖像的鏈接文本):
$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>';
}
}
你回答你的問題之前有人甚至迴應? – 2013-04-05 20:53:23
我剛剛使用了S.O擁有的問答功能。 – 2013-04-05 21:46:53
@Draew Baker。這是一個很好的代碼,但我會將查詢添加到[瞬態API](http://codex.wordpress.org/Transients_API),以便在每次加載頁面時保存查詢。特別是如果類別/ CPT /頁面不經常更改 – 2013-04-06 01:05:48