WP自定义帖子类型 - 将页面设置为父级

时间:2012-09-03 10:30:19

标签: wordpress

我的设置如下:
- 自定义帖子类型称为“客户” - 2级导航(单独,第二级仅显示当前页面是否有父/子)
- 一个名为Clients的页面 - 客户端帖子有一个自定义模板(single-clients.php)

我想让任何“客户”发布客户页面的子页面/子页面,以便导航在客户端页面上正确显示(它会自动列出子页面),并且可以轻松添加新客户端。

我找到了几个脚本,但没有一个完全符合我的要求。

这是我的子导航代码的主要部分:

<nav id='content_clients_navig' class='navig_general'>
    <ul>
        <?php
            global $post;

            //determine which navig should be displayed
            //if post has parent, display parent navig
            //else display the current post's navig
            $navig_display = ($post->post_parent) ? $post->post_parent : $post->ID;

            $menu_args = array(
                'child_of' => $navig_display,
                'title_li' => ''
            );
            wp_list_pages( $menu_args );
        ?>
    </ul>
</nav>

我通过在我的模板文件中插入这段代码来调用它:

<?php if (has_subnavig()) get_template_part( 'part', 'subnavig' ); ?>

这是has_subnavig:

function has_subnavig()
    {
        global $post;
        if( is_page() && $post->post_parent){
            return true;
        }else{
          $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
        };
        if($children){
          return true;
        }else{
          return false;
        };
    }

1 个答案:

答案 0 :(得分:0)

首先,您必须将has_subnavig更改为以下内容,以便在查看父级或客户端帖子时也显示子区域:

function has_subnavig()
{
    global $post;

    if ( is_page() && $post->post_parent ) {
        // post is child post
        return true;
    } else if ( $post->post_type == 'clients' || $post->post_name == 'clients' ) {
        // post is clients parent or clients post type
        return true;
    } else {
        // determine if post has children
        $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");

        if ( $children ) {
            return true;
        } else {
            return false;
        };
    };
}

其次,重要的是要知道,如果子导航应该是客户列表,还是标准子页面列表。这就是为什么我们会问,post_typepost_nameclients。我的子导航代码的主要部分可能是这样的:

global $post;

// determine which navig should be displayed
if ( $post->post_type == 'clients' || $post->post_name == 'clients' ) {
    // display clients navig if page is clients parent
    // or post_type is clients

    $menu_args = array(
        'child_of' => 0,
        'post_type' => 'clients',
        'post_status' => 'publish' 
    );
} else {
    // if post has parent, display parent navig
    // else display the current post's navig
    $navig_display = ($post->post_parent) ? $post->post_parent : $post->ID;

    $menu_args = array(
        'child_of' => $navig_display,
        'title_li' => ''
    );
}

wp_list_pages( $menu_args );
相关问题