• Hello, I would like to ask how the following thing can be done. I have the following categories

    News
    -Sport
    -Travel
    -Criminal

    Book
    -Fantasy
    -Criminal

    for example, if I enter the archive of a news category, how can I make the most from the top of the page also show the others sub categories Sport,Travel and Criminal

    and is this even possible?

    thanks in advance ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @joorkataa

    It is possible to display the subcategories of a parent category on the category archive page in WordPress. Here is one way you can do this:

    1. Open the category.php file.

    2. Find the code that displays the main category title. This might look something like this:

    <?php single_cat_title(); ?>

    3. Below this code, you can add the following to display the subcategories:

    <?php
      $current_category = get_category(get_query_var('cat'));
      $subcategories = get_categories( array(
          'parent' => $current_category->cat_ID,
          'hide_empty' => false
      ));
      if ( $subcategories ) :
    ?>
        <ul>
            <?php foreach ( $subcategories as $subcategory ) : ?>
                <li>
                    <a href="<?php echo get_category_link( $subcategory->term_id ); ?>"><?php echo $subcategory->name; ?></a>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    This code will retrieve the subcategories of the current category and display them in a list.

    ?? Note: Make sure to create a child theme and add this code to your child theme’s category.php template file, so your changes won’t be overwritten when the parent theme is updated.

    Thread Starter joorkataa

    (@joorkataa)

    Well, the code doesn’t work as it should

    It also shows me under categories in which there is nothing, and I want it to show under categories with only posts ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Category Show / Hide’ is closed to new replies.