• vralle

    (@vit-1)


    Hi!
    Its plugin-defoult (list-sub-categories-lsc) function for get subcategories:

    global $cat;

    // Get ancestors/parents (if there is any).
    $ancestors = get_ancestors( $cat, 'category' );

    // Get the first ancestor/parent (if there is one).
    $ancestor = $ancestors[0];

    // If we found an ancestor/parent (then we must be in a sub category).
    if($ancestor){

    // Get "children" to "ancestor/parent" (here we use $ancestor, since we are in a sub category).
    // Note: $ancestor is the main category id of the sub category id.
    foreach(get_categories("parent=$ancestor") as $category){
    // echo categories.
    echo 'term_id ) .'">'.$category->cat_name.'
    ';
    }

    }

    // If we did NOT find an ancestor (then we must be in a main category).
    if(!$ancestor){

    // Get "children" to "ancestor/parent" (here we use $cat, since we are in a main category).
    foreach(get_categories("parent=$cat") as $category){
    // echo cateogries.
    echo 'term_id ) .'">'.$category->cat_name.'
    ';
    }

    }

    all work, but here:
    1. not ID for curent category
    2. there was no link to the parent category
    I am not a programmer, but after week reading I get a work function, which I arranged for:

    global $cat;

    // Get ancestors/parents (if there is any).
    $ancestors = get_ancestors( $cat, 'category' );

    // Get the first ancestor/parent (if there is one).
    $ancestor = $ancestors[0];

    $child_cat = get_categories("parent=$cat");

    // If we found an ancestor/parent (then we must be in a sub category).
    if($ancestor){
    echo '<h1>'.get_cat_name($ancestor).'</h1>';
    ?>
    <nav>

      <?php
      foreach(get_categories("parent=$ancestor") as $category){
      if($cat == $category->term_id) $current = '-current'; else $current = '';
      // echo categories.
      echo '<li class="cat-item'.$current.'">term_id ) .'">'.$category->cat_name.'';
      }
      }

      // If we did NOT find an ancestor (then we must be in a main category).
      if(!$ancestor && $child_cat){
      ?>
      <h1><?php printf( single_cat_title( '', false ) . '' );?></h1>
      <nav>

      <?php
      // Get "children" to "ancestor/parent" (here we use $cat, since we are in a main category).
      foreach($child_cat as $category){
      // echo cateogries.
      echo '

    • term_id ) .'">'.$category->cat_name.'
    • ';
      }
      ?>

    Function is used in the template “Category” and can provide additional navigation for subcategories. like Apple 🙂
    But it remains no certainty with respect to the code. All the same I do not have to do with PHP or other programming languages​​, this is my first attempt.
    In the end, I raised two questions:
    1. My code is correct with respect to PHP and WP?
    2. How well chosen the function with respect to performance?

    Thanks.

    http://wordpress.org/extend/plugins/list-sub-categories-lsc/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Category navigation (list-sub-categories-lsc Moded)’ is closed to new replies.