• I am trying to only show the year sub categories. Every array I try does nothing or breaks the page. I want to have several drop downs that send the user to that category when selecting the year (as an example)

    Any help of where I am going wrong would be appreciated.

    <select name=”event-dropdown” onchange=’document.location.href=this.options[this.selectedIndex].value;’>
    <option value=””><?php echo esc_attr(__(‘Select Category’)); ?></option>

    <?php
    $categories = get_categories();
    foreach ($categories as $category) {

    $args = array(
    ‘cat’ => $catid,
    ‘post_type’ => ‘fallen-year’
    );
    $option .= ‘<option value=”‘.get_option(‘home’).’/category/’.$category->slug.'”>’;
    $option .= $category->cat_name;
    $option .= ‘ (‘.$category->category_count.’)’;
    $option .= ‘</option>’;
    }
    echo $option;
    ?>
    </select>

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You need the parent term ID of the subcategories. Depending on if you want all levels below the parent or just the immediate children below the parent, pass the parent ID as “parent” or “child_of” arguments.
    $categories = get_categories(['child_of'=> $parent_id,]);

    You cannot get categories only applied to a particular post type this way. If it’s imperative only years assigned to “fallen-year” be listed, you need to do an additional WP_Query for each term to see if there are any fallen-year posts using that term. This can become very time consuming. It’ll help a little if you do a direct SQL count query limited to one result per term to minimize the work the DB engine needs to do.

    Hello @webedelic

    You should check the function wp_get_archives

    I think there is a perfect snippet for you:

    <select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
      <option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option> 
      <?php wp_get_archives( array( 'type' => 'monthly', 'format' => 'option', 'show_post_count' => 1 ) ); ?>
    </select>

    I see a PHP error: you are using $catid but you don’t defined it in your snippet.

    You should add
    $catid = $category->term_id;
    before your array $args

    By the way, what is the purpose of this array?
    It is not used in your snippet

    You should also check WP_Term. Some of your properties could be wrong

    Tell me what’s up,

    Have a pleasant day,
    Clément M

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post Subcategories’ is closed to new replies.