WordPress的母公司接下来页面链接
问题描述:
这是一个自调Q & A.WordPress的母公司接下来页面链接
经常在WordPress的使用页面层次结构来构建项目结构。例如,在做投资组合的网站时,这是共同的:
- 工作
- BMW
- 模型
- 3系
- 5系
- 模型
- 奥迪 个
- 模型
- A3
- A4
- 模型
- BMW
因此,当用户的3系列页面上,往往你想有一个链接到“下一个汽车制造商”。你怎么能没有插件呢?
答
这些函数允许您设置要用于确定下一页的深度。所以在这个问题中,用户是'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