2012-01-11 8 views
0

私は現在、サイトメニューに基づいてセカンダリナビゲーションを出力するスクリプトを持っていますが、投稿をページにリンクするとは思えませんまだ二次ナビゲーションを表示することができます、これは可能ですか?ここで私のセカンダリナビゲーションを出力するために使用している現在のコードは次のとおりです:Wordpress - 投稿とページの自動セカンダリナビゲーションを出力する

<?php 
$secondAncestor = count($post->ancestors) -1; //figure out what level of navigation we are on, subtract one because we don't want to consider the top-level 
if($post->post_parent!=0) //if the page is not a top-level category 
{ 
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li class="sidebarlist">'; 
//the following lists children of second level ancestor of the current page. 
wp_list_pages("title_li=&child_of=".$post->ancestors[$secondAncestor]."& sort_column=menu_order&echo=1"); 
echo '</li>'; 
} 
else //if the page is a top-level category 
{ 
//listing only the child pages of the current section 
$children= wp_list_pages("title_li=&child_of=".$post->ID."& sort_column=menu_order&echo=0"); 
if($children) //this will stop it from displaying a section heading if there are no elements in the section (for example on the home page) 
{ 
echo '<nav><h2 class="widgettitle">In this section:</h2><ul class="secondary-nav"><li>'; 
echo $children; 

echo '</li>'; 

} 
} 
echo '</ul></nav>'; 
?> 
+0

あなたの最終結果は不明です。セカンダリナビゲーションがいつ/どのように表示されるかについて、もう少し説明してください。 –

+0

現在、現在のページと、現在のページと関係のある子を出力していますが、問題はページと投稿の関係を作成する方法がわかりません – Nick

答えて

1

ページは階層的であるため、保護者は親と子を持つことができます。 投稿はフラットで、カテゴリやタグによって関連しています。

投稿をページに関連付けるには、自分の投稿にカスタムフィールドを使用する必要があると思います。親ページIDと呼んで、サイドバーコードで、親ページID =現在のページのIDの投稿をチェックするカスタムクエリを追加できます。

+0

ありがとう、素晴らしいアイデアです! – Nick

0

チェックアウトFlexi Pages Widgetこれにより、サイドバーに高度に設定可能なサブページメニューが追加され、投稿に&ページを埋め込むことができます。

+0

ありがとうございますあなたはメニュー内の投稿を出力します。私が既に持っているコードはページだけです – Nick

関連する問題