2012-10-24 37 views
0

這是我用它來創建一個子菜單,列出所有自定義後我在WordPress創建代碼:加入主動/當前類到子菜單的自定義文章標題

<ul class="submenu"> 
     <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" /> 
     <!-- List post types --> 
     <?php 
      $the_query = new WP_Query('post_type=artworks_post'); 
      // The Loop 
      while ($the_query->have_posts()) : $the_query->the_post(); 
      echo '<li id="submenu_link" class="submenu_item_link">'; 
      echo '<a href="' .get_permalink(). '" > '; 
      the_title(); 
      echo '</a>'; 
      echo '</li>'; 
      endwhile; 
      // Reset Post Data 
      wp_reset_postdata(); 
     ?> 
     <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" /> 
    </ul> 

比如我目前的職位是artworks_post /項目 - 但它不會在子菜單中添加當前/活動類的標題(Project Coke標題)。

如何添加活動/當前類到子菜單中當前帖子的標題?

子菜單構成,即辦法讓自定義後...

回答

1

試試我的代碼? :

將這個代碼在你的functions.php

function if_current($s) { 
    global $wp_query,$post; 
    $current = $wp_query->get_queried_object_id(); 
    $post_id = $post->ID; 
    if($current==$post_id){echo $s;} 
} 

所以編輯喜歡你的代碼:

<ul class="submenu"> 
    <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" /> 
    <!-- List post types --> 
    <?php 
     $the_query = new WP_Query('post_type=artworks_post'); 
     // The Loop 
     while ($the_query->have_posts()) : $the_query->the_post(); 
     echo '<li id="submenu_link" class="submenu_item_link '; 
     if_current('current'); 
     echo '">'; 
     echo '<a href="' .get_permalink(). '" > '; 
     the_title(); 
     echo '</a>'; 
     echo '</li>'; 
     endwhile; 
     // Reset Post Data 
     wp_reset_postdata(); 
    ?> 
    <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" /> 
</ul> 

結果:

<li id="submenu_link" class="submenu_item_link current"> 
<a href="http://webkunst.comeze.com/test/artworks_post/project-coke/"> Project Coke</a> 
</li> 

PS:你可以添加任何if_current('text') in loop在你的主題,這將表明,當$post->ID == $wp_query->get_queried_object_id()(如電流)

所以,你可以在你的CSS使用類當前

+0

謝謝,我沒有你貼什麼,但看網頁,結果是不同的: (http://webkunst.comeze.com/test/artworks_post/project-coke/ – codek

+0

更新,你可以試試安吉安? @diego – l2aelba

+0

是的,它的工作原理!非常感謝你! – codek

0

你需要檢查你當前所在的頁面(或類別),並添加額外的類是活躍的子菜單項。您可以使用get_the_ID()來檢索WP循環中當前元素的ID。

所以這行之前:

echo '<li id="submenu_link" class="submenu_item_link">'; 

您將需要在當前帖子ID的帖子ID匹配添加一個檢查和添加一個類的li元素,這將表明。

0

我建議你使用WordPress菜單實現。 (Infos here and here

這是一個非常簡單的「應該如何」在WordPress中創建菜單的方法,例如,在您使用wp_nav_menu函數後自動標記活動項目。

如果您覺得被迫堅持自己的做法,您必須檢查當前顯示的帖子是否等於您在循環中顯示的帖子之一,並且回顯類如當前或活動。

相關問題