2014-07-24 3 views
1

저는 Wordpress의 wp_list_pages를 사용하여 사이트의 페이지 탐색을 표시하고 있습니다.Wordpress - wp_list_page nav에서 클래스 이름을 변경하는 방법

일부 페이지에는 자식이 있으므로 해당 링크 아래에 드롭 다운 메뉴가 필요합니다.

HTML 내가 탐색 및 드롭 다운 메뉴를 만들 수 부트 스트랩 사용하고 싶었이

<ul class=""> 
    <li class="page_item page-item-6 current_page_item"><a href="/">Home</a></li> 
    <li class="page_item page-item-6 current_page_item"><a href="/">Profile</a></li> 
    <li class="page_item page-item-8 page_item_has_children dropdown"> 
     <a href="">Products &#038; Services</a> 
     <ul class='children'> 
      <li class="page_item page-item-17"><a href="">Buying</a></li> 
      <li class="page_item page-item-19"><a href="">Selling</a></li> 
      <li class="page_item page-item-23"><a href="">Managing</a></li> 
     </ul> 
    </li> 
</ul> 

같은 것입니다. 같은

뭔가 - http://www.ttmt.org.uk/nav/

이에 대한 HTML은 다음과 같이한다.

<ul class="nav navbar-nav"> 
    <li><a href="#">Home</a></li> 
    <li><a href="#">Profile</a></li> 
    <li class="dropdown"> 
     <a href="#" data-toggle="dropdown" class="dropdown-toggle">Messages <b class="caret"></b></a> 
     <ul class="dropdown-menu"> 
      <li><a href="#">Inbox</a></li> 
      <li><a href="#">Drafts</a></li> 
      <li><a href="#">Sent Items</a></li> 
     </ul> 
    </li> 
</ul> 

하위 페이지가 포함 된 li 및 ul을 'dropdown'클래스에 추가해야합니다. 또한 'a'태그에 클래스와 데이터 토글 속성을 추가해야합니다.

wp_list_pages를 사용하고 nav가 동적으로 생성 될 때 어떻게 이러한 클립을 추가 할 수 있습니까?

내가 자식 페이지 containin 리튬에 '드롭'을 추가하려면이 기능을 사용하고

내가 extened이 기능은 내가이 기능을 달성하기 위해

답변

4

을 필요로하는 다른 클래스를 추가 할 수있는 방법을
function add_parent_class($css_class, $page, $depth, $args){ 
    if (! empty($args['has_children'])) 
     $css_class[] = 'dropdown'; 
    return $css_class; 
} 
add_filter('page_css_class', 'add_parent_class', 10, 4);    

, 우리 필터 만 사용할 수는 없습니다. WordPress의 Walker 클래스를 확장해야합니다.

나는이 같은 wp_list_pages라는 것을 가정입니다 :

$args = array('authors' => '', 
    'child_of' => 0, 
    'date_format' => get_option('date_format'), 
    'depth' => 0, 
    'echo' => 1, 
    'exclude' => '', 
    'include' => '', 
    'link_after' => '', 
    'link_before' => '', 
    'post_type' => 'page', 
    'post_status' => 'publish', 
    'show_date' => '', 
    'sort_column' => 'menu_order, post_title', 
    'sort_order' => '', 
    'title_li' => __(''), 
    'walker' => '', 
); 

echo '<ul class="nav navbar-nav">' . wp_list_pages($args) . '</ul>' ; 

우리는 워커 있었던 파라미터에 값을 전달합니다. 그 매개 변수는 우리가 지금 만들 클래스의 객체가 될 것입니다. 테마의 functions.php 또는 클래스 위 site specific plugin

class Wdm_Walker_Page extends Walker_Page { 

/** 
* @see Walker::start_lvl() 
* @since 2.1.0 
* 
* @param string $output Passed by reference. Used to append additional content. 
* @param int $depth Depth of page. Used for padding. 
* @param array $args 
*/ 
function start_lvl(&$output, $depth = 0, $args = array()) { 
    $indent = str_repeat("\t", $depth); 
    $output .= "\n$indent<ul class='dropdown-menu children'>\n"; 
} 

/** 
* @see Walker::start_el() 
* @since 2.1.0 
* 
* @param string $output Passed by reference. Used to append additional content. 
* @param object $page Page data object. 
* @param int $depth Depth of page. Used for padding. 
* @param int $current_page Page ID. 
* @param array $args 
*/ 
function start_el(&$output, $page, $depth = 0, $args = array(), $current_page = 0) { 
    if ($depth) 
     $indent = str_repeat("\t", $depth); 
    else 
     $indent = ''; 

    extract($args, EXTR_SKIP); 
    $css_class = array('page_item', 'page-item-'.$page->ID); 

    if(isset($args['pages_with_children'][ $page->ID ])) 
     $css_class[] = 'page_item_has_children dropdown'; 

    if (!empty($current_page)) { 
     $_current_page = get_post($current_page); 
     if (in_array($page->ID, $_current_page->ancestors)) 
      $css_class[] = 'current_page_ancestor'; 
     if ($page->ID == $current_page) 
      $css_class[] = 'current_page_item'; 
     elseif ($_current_page && $page->ID == $_current_page->post_parent) 
      $css_class[] = 'current_page_parent'; 
    } elseif ($page->ID == get_option('page_for_posts')) { 
     $css_class[] = 'current_page_parent'; 
    } 

    /** 
    * Filter the list of CSS classes to include with each page item in the list. 
    * 
    * @since 2.8.0 
    * 
    * @see wp_list_pages() 
    * 
    * @param array $css_class An array of CSS classes to be applied 
    *        to each list item. 
    * @param WP_Post $page   Page data object. 
    * @param int  $depth  Depth of page, used for padding. 
    * @param array $args   An array of arguments. 
    * @param int  $current_page ID of the current page. 
    */ 
    $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page, $depth, $args, $current_page)); 

    if ('' === $page->post_title) 
     $page->post_title = sprintf(__('#%d (no title)'), $page->ID); 

    /** This filter is documented in wp-includes/post-template.php */ 
      if(preg_match('/dropdown/', $css_class) != FALSE){ 
    $output .= $indent . '<li class="' . $css_class . '"><a data-toggle="dropdown" class="dropdown-toggle" href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters('the_title', $page->post_title, $page->ID) . $link_after . '</a>'; 
      } 
      else{ 
       $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters('the_title', $page->post_title, $page->ID) . $link_after . '</a>'; 
      } 

    if (!empty($show_date)) { 
     if ('modified' == $show_date) 
      $time = $page->post_modified; 
     else 
      $time = $page->post_date; 

     $output .= " " . mysql2date($date_format, $time); 
    } 
    } 
} 

에이 클래스를 추가 당신이 원하는 모든 클래스를 추가합니다.

이제이 클래스의 객체를 wp_list_pages에 전달해야합니다. 그래서 이런 식으로 보일 것입니다

$args = array(
        'authors' => '', 
        'child_of' => 0, 
        'date_format' => get_option('date_format'), 
        'depth' => 0, 
        'echo' => 1, 
        'exclude' => '', 
        'include' => '', 
        'link_after' => '', 
        'link_before' => '', 
        'post_type' => 'page', 
        'post_status' => 'publish', 
        'show_date' => '', 
        'sort_column' => 'menu_order, post_title', 
        'sort_order' => '', 
        'title_li' => __(''), 
        'walker' => new Wdm_Walker_Page() 
       ); 

echo '<ul class="nav navbar-nav">' . wp_list_pages($args) . '</ul>' ; 

나는 그것이 도움이되기를 바랍니다! 더 이상 필터 page_css_class에 코드를 작성할 필요가 없습니다. :)

관련 문제