admin管理员组文章数量:1130349
I have a custom post type that is hierarchical. I have 3 level of pages, I need a way to detect whether I am on the top level, 2nd level or 3rd level. I've looked into this and can only find examples that work with is_page() but apparently that doesn't work with CPTs.
function nldf_gallery() {
$labels = array(
'name' => 'NLDF',
'singular_name' => 'NLDF',
'add_new' => 'Add New',
'add_new_item' => 'Add New NLDF',
'edit_item' => 'Edit NLDF',
'new_item' => 'New NLDF',
'all_items' => 'All NLDF',
'view_item' => 'View NLDF',
'search_items' => 'Search NLDF',
'not_found' => 'No NLDF found',
'not_found_in_trash' => 'No NLDF found in Trash',
'menu_name' => 'NLDF'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'has_archive' => false,
'menu_position' => 4,
'menu_icon' => 'dashicons-format-gallery',
'supports' => array('title','editor','comments','revisions','page-attributes')
);
register_post_type('nldf', $args);
}
add_action('init', 'nldf_gallery');
OR - A way to use a different template for each level of pages
I have a custom post type that is hierarchical. I have 3 level of pages, I need a way to detect whether I am on the top level, 2nd level or 3rd level. I've looked into this and can only find examples that work with is_page() but apparently that doesn't work with CPTs.
function nldf_gallery() {
$labels = array(
'name' => 'NLDF',
'singular_name' => 'NLDF',
'add_new' => 'Add New',
'add_new_item' => 'Add New NLDF',
'edit_item' => 'Edit NLDF',
'new_item' => 'New NLDF',
'all_items' => 'All NLDF',
'view_item' => 'View NLDF',
'search_items' => 'Search NLDF',
'not_found' => 'No NLDF found',
'not_found_in_trash' => 'No NLDF found in Trash',
'menu_name' => 'NLDF'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'has_archive' => false,
'menu_position' => 4,
'menu_icon' => 'dashicons-format-gallery',
'supports' => array('title','editor','comments','revisions','page-attributes')
);
register_post_type('nldf', $args);
}
add_action('init', 'nldf_gallery');
OR - A way to use a different template for each level of pages
Share Improve this question edited Dec 20, 2018 at 14:59 RiddleMeThis asked May 25, 2016 at 13:27 RiddleMeThisRiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges1 Answer
Reset to default 2You can call https://codex.wordpress/Function_Reference/get_ancestors within your template. It'll return an array of ancestors to your current post and you can use the length of the array to see how deep you are in your hierarchy.
Use it at the top of your template file to set a variable $post_depth and use that to adjust your body_class for targeted CSS or to conditionally include template parts.
Something like this would work:
<?php
// single-nldf.php
// Template for displaying single NLDFs
$level = count( get_ancestors( get_queried_object_id(), 'nldf' ) );
add_filter('body_class','nldf_page_class_names');
function nldf_page_class_names($classes) {
// add 'class-name' to the $classes array
$classes[] = 'single-nldf-' . $level;
// return the $classes array
return $classes;
}
get_header( 'nldf-' . $level );
// loads header-nldf-0.php, header-nldf-1.php or header-nldf-2.php
get_template_part( 'loop', 'nldf-' . $level );
// loads loop-nldf-0.php, loop-nldf-1.php or loop-nldf-2.php
get_footer( 'nldf-' . $level );
// loads footer-nldf-0.php, footer-nldf-1.php or footer-nldf-2.php
For robustness you might like to check for posts nested in deeper levels and do something appropriate.
I have a custom post type that is hierarchical. I have 3 level of pages, I need a way to detect whether I am on the top level, 2nd level or 3rd level. I've looked into this and can only find examples that work with is_page() but apparently that doesn't work with CPTs.
function nldf_gallery() {
$labels = array(
'name' => 'NLDF',
'singular_name' => 'NLDF',
'add_new' => 'Add New',
'add_new_item' => 'Add New NLDF',
'edit_item' => 'Edit NLDF',
'new_item' => 'New NLDF',
'all_items' => 'All NLDF',
'view_item' => 'View NLDF',
'search_items' => 'Search NLDF',
'not_found' => 'No NLDF found',
'not_found_in_trash' => 'No NLDF found in Trash',
'menu_name' => 'NLDF'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'has_archive' => false,
'menu_position' => 4,
'menu_icon' => 'dashicons-format-gallery',
'supports' => array('title','editor','comments','revisions','page-attributes')
);
register_post_type('nldf', $args);
}
add_action('init', 'nldf_gallery');
OR - A way to use a different template for each level of pages
I have a custom post type that is hierarchical. I have 3 level of pages, I need a way to detect whether I am on the top level, 2nd level or 3rd level. I've looked into this and can only find examples that work with is_page() but apparently that doesn't work with CPTs.
function nldf_gallery() {
$labels = array(
'name' => 'NLDF',
'singular_name' => 'NLDF',
'add_new' => 'Add New',
'add_new_item' => 'Add New NLDF',
'edit_item' => 'Edit NLDF',
'new_item' => 'New NLDF',
'all_items' => 'All NLDF',
'view_item' => 'View NLDF',
'search_items' => 'Search NLDF',
'not_found' => 'No NLDF found',
'not_found_in_trash' => 'No NLDF found in Trash',
'menu_name' => 'NLDF'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'has_archive' => false,
'menu_position' => 4,
'menu_icon' => 'dashicons-format-gallery',
'supports' => array('title','editor','comments','revisions','page-attributes')
);
register_post_type('nldf', $args);
}
add_action('init', 'nldf_gallery');
OR - A way to use a different template for each level of pages
Share Improve this question edited Dec 20, 2018 at 14:59 RiddleMeThis asked May 25, 2016 at 13:27 RiddleMeThisRiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges1 Answer
Reset to default 2You can call https://codex.wordpress/Function_Reference/get_ancestors within your template. It'll return an array of ancestors to your current post and you can use the length of the array to see how deep you are in your hierarchy.
Use it at the top of your template file to set a variable $post_depth and use that to adjust your body_class for targeted CSS or to conditionally include template parts.
Something like this would work:
<?php
// single-nldf.php
// Template for displaying single NLDFs
$level = count( get_ancestors( get_queried_object_id(), 'nldf' ) );
add_filter('body_class','nldf_page_class_names');
function nldf_page_class_names($classes) {
// add 'class-name' to the $classes array
$classes[] = 'single-nldf-' . $level;
// return the $classes array
return $classes;
}
get_header( 'nldf-' . $level );
// loads header-nldf-0.php, header-nldf-1.php or header-nldf-2.php
get_template_part( 'loop', 'nldf-' . $level );
// loads loop-nldf-0.php, loop-nldf-1.php or loop-nldf-2.php
get_footer( 'nldf-' . $level );
// loads footer-nldf-0.php, footer-nldf-1.php or footer-nldf-2.php
For robustness you might like to check for posts nested in deeper levels and do something appropriate.
本文标签: Detect if 1st2nd or 3rd level custom page
版权声明:本文标题:Detect if 1st, 2nd or 3rd level custom page? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749080942a2313040.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论