Wordpress - 如果是第1 /第2 /第3级自定义页面?

时间:2016-05-24 21:14:45

标签: wordpress wordpress-theming custom-post-type

我有一个分层的自定义帖子类型。我有3级页面,我需要一种方法来检测我是在顶级,第二级还是第三级。我已经研究了这个,只能找到与is_page()一起使用的例子,但显然不适用于CPT。

    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');

谢谢!

1 个答案:

答案 0 :(得分:0)

从wp stackexchange中获得Andy Macaulay-Brook的答案,但是想在这里发帖,任何人都能找到它。

$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
相关问题