2012-09-03 20 views
0

我的設置如下:
- 自定義後類型,稱爲「客戶」
- 2導航級別(獨立,第二級只顯示如果當前頁面有父母/子女)
- 一個頁面調用客戶
- 客戶端的職位有一個自定義模板(單clients.php)WP自定義後類型 - 設置頁面父

我要讓任何「客戶」發佈客戶端頁面的子頁/孩子,所以導航顯示器正確地在客戶端頁面(它會自動列出子頁面),並且很容易添加新的客戶端。

我發現了幾個腳本,但他們都沒有做到我想要的。

這裏是我的子navig代碼的主要部分:

<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; 
     }; 
    } 
+0

你是如何列出你的二級導航?也許你需要更改該代碼。你使用WordPress的菜單? – martinczerwi

+0

我編輯的主要帖子,檢查出:) –

+0

任何其他的想法? –

回答

0

首先,您必須將has_subnavig更改爲以下內容,以便subn當觀看的父母,或客戶端的帖子AV還顯示:

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); 
+0

感謝您的幫助。雖然有一點小振作。當我點擊一個客戶端時,我不再在主導航中獲得'current_page_item'類,這非常重要。此外,subnavig顯示完整的頁面列表,我不明白。 –

+0

顯示客戶端時,$ post-> post_type不輸出任何內容。 –