Multi-Level Category Browsing With WordPress

Submitted by Matthew on Tue, 02/02/2016 - 19:03

Now, I am primarily a native PHP and Drupal developer, and this blog focuses on PHP and Drupal.  But never having explored WordPress much in the past, I decided this would be as good a time as any to do so.  So I decided to build a WordPress plugin.

A friend of mine who likes to peruse various blogs mentioned to me some time ago that a feature she would like to see in blogs is a way of browsing top-level categories in one select box and then having whatever top-level category she selects populates a second select box with the subcategories of that top-level category.  This would make it easier to sort through parent and child categories  in blogs that have, for example, dozens of top-level categories and many more subcategories.  And whether she selects from the top-level or subcategories, the page would then display all blog posts belonging to whichever category she selected.

This idea sounded good and useful to me, so I decided to create WordPress plugin called "Categories & Subcategories" to do this.  Actually, I decided to create a widget that could be placed in various regions of a WordPress theme, so I had to create a plugin to define the widget.  

The WordPress core "Categories" widget displays a select box (among other alternatives) for the user to select any category of any level and then displays all blog posts from the selected category, so I used the code for this widget as my starting point.  There's no particular need for me to write a tutorial on how to write WordPress plugins or use WordPress-specific functions, because so many of these exist already.  But if you're interested in looking at the code or using the plugin, it can be found on my github account here:  https://github.com/mckinzie25/cats-and-subcats-select-widget

A demo for this widget exists online at http://wordpress.webmechanic.org  The widget itself is in the top-right corner, right above the core "Categories" widget.  You'll notice that whatever category you select in my widget also populated the "Categories" select box, because both widgets pull the current category from the query string.

One more word on WordPress (for the moment):  WordPress still hashes passwords using MD5 by default.  This is not secure.  The good people at Roots have built a plugin to use the much stronger bcrypt hashing function--anyone with a WordPress site should use this bcrypt plugin to enhance the security of their site.  Check it out here:  https://roots.io/improving-wordpress-password-security
 

Tags

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.