2016-09-08 15 views
0

wp_list_pagesでカスタムウォーカーを使用しています。子供がいる場合には$ linkと$ checkchildrenの値を変更したいと思います。wp_list_pages子供がいないか確認してください

私はそれが子供

$children = wp_list_pages(array(
    'sort_column' => 'menu_order', 
    'title_li' => '', 
    'echo'  => 1, 
    'walker' => new Sidebar_Custom_Walker() 
)); 




class Sidebar_Custom_Walker extends Walker_Page { 
    function start_el(&$output, $page, $depth, $args, $current_page = 0) { 
     $output .= $indent . '<li class="rtChild">'; 
     $linkcss ="c1"; // should be blank if no children 
     $output .= '<a href="' . get_permalink($page->ID) . '" class="'.$linkcss .'">' . $link_before; 
     $output .= apply_filters('the_title', $page->post_title, $page->ID); 
     $output .= $link_after . '</a> 
     $checkchildren = '<span class="plusBtn"></span>'; // should be blank if no children 
     $output .= $checkchildren; 
    } 
} 

答えて

0

を持っていない場合は、これを行う簡単な方法は、方法start_el内WordPressの組み込み関数get_pages()を使用することです$ linkcssと$ checkchildrenがnullになりたいです。これを壊すのをしてみましょう:

最初の部分:

<?php 

    class Sidebar_Custom_Walker extends Walker_Page { 

     function start_el(&$output, $page, $depth, $args, $current_page = 0) { 

      $output    .= $indent . '<li class="rtChild">'; 

新しいパーツ:

  // Retrieve page children with get_pages() 
      // in combination with current $page->ID. 
      $page_children  = get_pages(array('child_of' => $page->ID)); 

      // Count amount of pages 
      $page_children_count = count($page_children); 

      // Does this page have children? 
      if($page_children_count > 0){ 
       // Page has children 
       $linkcss   = "c1"; 
       $checkchildren = '<span class="plusBtn"></span>'; 
      }else{ 
       // Page has no children 
       $linkcss   = null; 
       $checkchildren = null; 
      } 

そして、我々の出力は同じまま:

  $output .= '<a href="' . get_permalink($page->ID) . '" class="'.$linkcss .'">' . $link_before; 
      $output .= apply_filters('the_title', $page->post_title, $page->ID); 
      $output .= $link_after . '</a>'; 
      $output .= $checkchildren; 

     } 

    } 


?> 

私はありません文字列を無効にしてから文字列を使用するのがよいかどうかを確認してください変数は後で$outputとなります。

個人的に私は空にするためにそれらを設定します:

$linkcss  = ""; 
$checkchildren = ""; 
関連する問題