Forums

[resolved] Displaying categories conditional on their parent-cat? (4 posts)

  1. liberty50
    Member
    Posted 2 years ago #

    Hi guys,

    I am trying to create some custom navigation for a site, where categories are displayed only within the current "family". I have the call to list the pages working fine:

    <?php wp_list_categories('child_of=112&title_li=') ?>

    I also found some code within this forum that works perfectly to conditionally display only on related pages(in this instance my top level category is id=112):

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args = array(
        'include' => $cat,
    	  'hide_empty' => 0
    	  );
      $categories = get_categories($args);
      if ( ($cat == 112) || ($categories[0]->category_parent == 112) ) {
    
      }
    }
    ?>

    I've tested both components and they work great separately, but as soon as I try to combine them the pages fails to load. Here is what I have:

    <?php
    if ( is_category() ) {
      $cat = get_query_var('cat');
      $args = array(
        'include' => $cat,
    	  'hide_empty' => 0
    	  );
      $categories = get_categories($args);
      if ( ($cat == 112) || ($categories[0]->category_parent == 112) ) {
    <?php wp_list_categories('child_of=112&title_li=') ?>
      }
    }
    ?>

    My understanding of php is rudimentary at best and I'm guessing that this will be a simple syntactic issue. If anyone could offer advice, It would be greatly appreciated.

  2. hedonplay
    Member
    Posted 2 years ago #

    <?php wp_list_categories('child_of=112&title_li=') ?>

    you missed the ";" in the end.

  3. liberty50
    Member
    Posted 2 years ago #

    Thank you!

    I knew it was going to be something simple. It's working perfectly now.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

    You shouldn't have..

    <?php wp_list_categories('child_of=112&title_li=') ?>

    but..

    wp_list_categories('child_of=112&title_li=');

    The opening and closing tags shouldn't be present because you're already in PHP mode so to speak (open at the top <?php and closed at the bottom ?>).

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags