How to Hide or Style Your Subcategories in WordPress

4 days ago, WordPress Tutorials, Views
How to Hide or Style Your Subcategories in WordPress

Understanding Subcategories in WordPress

Subcategories are a hierarchical feature in WordPress that allow you to organize your content into more specific groups within broader categories. Think of categories as the main folders in a file system and subcategories as the subfolders within those folders. This structure is beneficial for both site visitors and search engine optimization (SEO).

  • Improved User Experience: Subcategories make it easier for users to navigate your website and find the information they are looking for.
  • Better SEO: Search engines like Google use your site’s structure to understand the relationships between different pieces of content. Well-organized subcategories can improve your site’s ranking.
  • Content Management: Subcategories help you to manage and organize your content efficiently on the backend of your website.

However, there are situations where you might want to hide or style your subcategories. Perhaps the default appearance doesn’t match your theme, or you want to present them in a unique way. Maybe you want to only show them under certain circumstances.

Reasons for Hiding or Styling Subcategories

Before diving into the “how,” let’s explore some common reasons why you might want to hide or style your WordPress subcategories.

  • Design Consistency: The default subcategory styling may clash with your overall website design.
  • Simplified Navigation: You might prefer a simpler main navigation menu and want to present subcategories in a different way, such as within the body of a relevant post or page.
  • SEO Considerations: You might believe that displaying too many categories and subcategories can dilute your site’s link equity.
  • Specific Use Cases: Some websites have specific requirements, such as hiding subcategories that are still under development or reserved for future content.
  • Cluttered Appearance: If you have a very large number of subcategories, the default display could look overwhelming.

Methods to Hide Subcategories in WordPress

There are several methods you can use to hide subcategories in WordPress, ranging from simple CSS solutions to more advanced PHP code modifications. Remember to back up your website before making any code changes.

Using CSS to Hide Subcategories

CSS (Cascading Style Sheets) is the easiest way to hide subcategories, as it doesn’t involve directly altering your WordPress theme files. You can use the WordPress Customizer or add CSS to your theme’s stylesheet (usually style.css).

  1. Identify the Subcategory CSS Class or ID: Use your browser’s developer tools (usually accessible by pressing F12) to inspect the HTML structure of your category archive page or wherever the subcategories are displayed. Look for a CSS class or ID associated with the subcategories. Common classes might include `category-children`, `sub-menu`, or similar.
  2. Add the CSS Code: Once you’ve identified the correct class or ID, add the following CSS code to your WordPress Customizer (Appearance > Customize > Additional CSS) or your theme’s stylesheet:

    `/* Example: Hiding subcategories with class ‘category-children’ */
    .category-children {
    display: none;
    }`

    Replace `.category-children` with the actual class or ID you found.

    To hide all subcategories everywhere, you could use a more generic selector, but be cautious as this might affect other elements on your site:

    `.cat-item ul {
    display: none;
    }`

    This CSS will hide all unordered lists (`ul`) within list items (`li`) with the class `cat-item`, which is a common way subcategories are structured.

  3. Test and Adjust: After adding the CSS code, check your website to ensure the subcategories are hidden as expected. You might need to adjust the CSS selector if it’s not working correctly.

Using PHP Code to Hide Subcategories

For more granular control over which subcategories are hidden and where they are hidden, you can use PHP code. This method requires modifying your theme’s functions.php file or using a custom plugin. Be very careful when editing theme files, as errors can break your website. Always create a child theme to avoid losing your changes when the main theme is updated.

  1. Create a Child Theme (Recommended): If you don’t already have one, create a child theme. This is a safe way to modify your theme without affecting the original files.
  2. Edit functions.php: Open the functions.php file in your child theme’s directory.
  3. Add the PHP Code: Add the following PHP code to hide subcategories. This code uses a WordPress filter to modify the output of the `wp_list_categories()` function, which is often used to display categories and subcategories.

    `function hide_subcategories($output, $args) {
    if (isset($args[‘parent’]) && $args[‘parent’] != 0) { // Only target subcategories
    return ”; // Return an empty string to hide them
    }
    return $output;
    }
    add_filter(‘wp_list_categories’, ‘hide_subcategories’, 10, 2);`

    This code will effectively remove all subcategories from being displayed when using the `wp_list_categories()` function.

    To hide specific subcategories, you can modify the code to check the category ID. First, you’ll need to find the ID of the subcategory you want to hide. You can find this by going to Posts > Categories in your WordPress dashboard and hovering over the subcategory you want to hide. The ID will be in the URL.

    `function hide_specific_subcategories($output, $args) {
    $hidden_categories = array(5, 10, 15); // Array of category IDs to hide

    if (isset($args[‘taxonomy’]) && $args[‘taxonomy’] == ‘category’) {
    $categories = get_categories($args);
    foreach ($categories as $category) {
    if (in_array($category->term_id, $hidden_categories)) {
    $output = str_replace(‘

  4. term_id . ‘” style=”display:none;”>’, $output);
    }
    }
    }

    return $output;
    }
    add_filter(‘wp_list_categories’, ‘hide_specific_subcategories’, 10, 2);`

    Replace `array(5, 10, 15)` with an array of the category IDs you want to hide. This code modifies the output of `wp_list_categories` to add `style=”display:none;”` to the list item containing the specified category IDs. This effectively hides them using CSS.

  5. Test and Adjust: Save the functions.php file and check your website to ensure the subcategories are hidden as expected. If you encounter any errors, double-check your code for syntax errors or typos.

Using a Plugin to Hide Subcategories

Several WordPress plugins can help you hide or manage your subcategories without writing code. This is often the easiest and safest option for non-developers.

  1. Search for a Plugin: Go to Plugins > Add New in your WordPress dashboard and search for plugins like “Category Order and Taxonomy Terms Order” (which also allows hiding), “Hide Categories,” or similar plugins.
  2. Install and Activate the Plugin: Choose a plugin with good reviews and a recent update. Install and activate the plugin.
  3. Configure the Plugin: Follow the plugin’s instructions to configure it to hide or manage your subcategories. Most plugins will provide an easy-to-use interface for selecting which categories to hide.

    For example, some plugins might offer a drag-and-drop interface for reordering categories and subcategories, with an option to hide specific items.

    Other plugins might provide a setting to hide empty categories (including subcategories) or to hide categories from specific areas of your website.

  4. Test and Adjust: Check your website to ensure the plugin is working correctly and that the subcategories are hidden as expected.

Methods to Style Subcategories in WordPress

Instead of hiding subcategories, you might want to style them to better match your website’s design.

Using CSS to Style Subcategories

As with hiding subcategories, CSS is the most common way to style them.

  1. Identify the Subcategory CSS Class or ID: Use your browser’s developer tools to inspect the HTML structure of your category archive page or wherever the subcategories are displayed. Look for a CSS class or ID associated with the subcategories.
  2. Add the CSS Code: Add the following CSS code to your WordPress Customizer (Appearance > Customize > Additional CSS) or your theme’s stylesheet:

    `/* Example: Styling subcategories with class ‘category-children’ */
    .category-children {
    margin-left: 20px; /* Indent subcategories */
    font-style: italic; /* Make the text italic */
    color: #888; /* Change the text color */
    }`

    Replace `.category-children` with the actual class or ID you found.

    Here are some other common styling options:

    * Changing the font size: `font-size: 0.8em;`
    * Adding a background color: `background-color: #f0f0f0;`
    * Adding a border: `border: 1px solid #ccc;`
    * Changing the font weight: `font-weight: bold;`

    You can combine these styles to create a unique look for your subcategories.

  3. Test and Adjust: After adding the CSS code, check your website to ensure the subcategories are styled as expected. You might need to adjust the CSS selectors or styles if they’re not working correctly.

Using PHP Code to Style Subcategories

You can also use PHP code to add custom CSS classes to your subcategories, which you can then style using CSS.

  1. Create a Child Theme (Recommended): Create a child theme if you don’t already have one.
  2. Edit functions.php: Open the functions.php file in your child theme’s directory.
  3. Add the PHP Code: Add the following PHP code to add a custom CSS class to subcategories:

    `function add_subcategory_class($category) {
    if ($category->category_parent != 0) {
    $category->category_css_class = ‘subcategory’;
    }
    return $category;
    }
    add_filter(‘category_template_category_description’, ‘add_subcategory_class’);`

    This code adds the class “subcategory” to all subcategories.

    You can then style this class in your CSS file:

    `.subcategory {
    font-weight: bold;
    color: blue;
    }`

    This will make the subcategory text bold and blue.

  4. Test and Adjust: Save the functions.php file and check your website to ensure the subcategories are styled as expected.

Using a Plugin to Style Subcategories

Some WordPress plugins can also help you style your subcategories. These plugins often provide more advanced styling options than you can achieve with CSS alone.

  1. Search for a Plugin: Go to Plugins > Add New in your WordPress dashboard and search for plugins like “Category Styling” or similar plugins.
  2. Install and Activate the Plugin: Choose a plugin with good reviews and a recent update. Install and activate the plugin.
  3. Configure the Plugin: Follow the plugin’s instructions to configure the plugin to style your subcategories. These plugins often provide a visual interface for customizing the appearance of your categories and subcategories.
  4. Test and Adjust: Check your website to ensure the plugin is working correctly and that the subcategories are styled as expected.

Best Practices

  • Back Up Your Website: Before making any changes to your theme files or database, always back up your website. This will allow you to restore your website to its previous state if something goes wrong.
  • Use a Child Theme: When modifying your theme’s files, always use a child theme. This will prevent your changes from being overwritten when the main theme is updated.
  • Test Thoroughly: After making any changes, test your website thoroughly to ensure that everything is working as expected.
  • Consider User Experience: When hiding or styling subcategories, always consider the user experience. Make sure that your website is still easy to navigate and that users can easily find the information they are looking for.
  • Use CSS Selectors Wisely: Be specific with your CSS selectors to avoid affecting other elements on your website.
  • Document Your Changes: Keep track of any changes you make to your theme files or database so that you can easily undo them if necessary.