Dropdown Navigation: How one can Keep the Chosen Possibility On WordPress

Dropdown Navigation: How one can Keep the Chosen Possibility On WordPress

In a earlier tutorial, we realized use a choose factor for navigation and keep the chosen possibility on web page load. To reveal it we used an instance venture with some static pages.

Right this moment, we’ll go one step additional and focus on the actions wanted for incorporating this performance right into a WordPress web site.

Be aware: this tutorial assumes you’re accustomed to WordPress in addition to the method of constructing youngster themes.

1. Create a Little one Theme

To kick issues off we’ll create a new WordPress web site and run it regionally by XAMPP. Subsequent, we create a new youngster theme for the Twenty Fifteen theme (v2.1 on the time of this writing).

Its preliminary file construction will appear like this:

The initial file structure of our theme

Let’s have a more in-depth have a look at the contents of our youngster theme.

The Stylesheet

The kid theme’s stylesheet (“type.css”) begins with the next file header (you possibly can enrich it relying in your wants, however for our functions it’s good to go):

Inside this file we put types focusing on the choose factor which we’ll be including in an upcoming part.

For now, let’s add these types, most of which come from the earlier tutorial:

Enqueuing Information

Contained in the “capabilities.php” file we enqueue the father or mother and youngster theme stylesheets. Along with these, we enqueue a JavaScript file which can maintain our JavaScipt code.

Right here’s the required PHP code:


The “screenshot.png” is a 1200x900px thumbnail of the theme design.


The “property” folder incorporates the photographs and JavaScript recordsdata that we’ll want for our web site. On a bigger venture it would include different folders as effectively, like a “scss” folder.


Contained in the “property” folder, there’s the “foremost.js” file which incorporates the code that ought to run every time an possibility is chosen. 

Right here’s the required code:

Be aware: This code is taken from the earlier tutorial, the place we walked by the performance and defined the way it works. The one small addition right here is the preliminary if assertion which checks to see whether or not the web page incorporates a choose factor or not. That occurs as a result of, as we’ll see, the choose will seem solely on sure pages. As an alternative of including this additional verify, an alternate method is to conditionally enqueue the JavaScript file.

2. Activate the Little one Theme

Now that we’re accustomed to the kid theme’s contents, we’re able to activate it.

Activating the child theme

3. Create Some Posts and Classes

As a subsequent step we’ll create 4 totally different submit classes:

The post categories

With the classes in place, we’ll add a bunch of posts and randomly assign them to the aforementioned classes, like this:

The posts

Now that we’ve added content material, let’s have a look at the looks of the class (archive) pages.

4. Customise the Class Pages

Inside every of the class pages, we need to add a choose factor which can maintain all of the submit classes. Every time an possibility is chosen, the associated class ought to be loaded and the goal possibility ought to be marked as chosen. Plus, an additional All possibility ought to seem which can load all posts (by default the homepage shows all posts).

With that in thoughts, we first loop by all submit classes and add them as choices to a choose factor.

The code accountable for that conduct is as follows:

We put this code inside a file referred to as “content-post-categories.php”. This file is saved in a listing referred to as “template-parts”.

We need to import the goal file within the “index.php” and “archive.php” father or mother recordsdata. That stated, we’ll need to override these recordsdata and add our customized code.

To do that, we first copy and paste these recordsdata in our youngster theme. The brand new construction of our theme turns into as follows:

The new theme structure

Subsequent within the “index.php” file we add this code:

Overriding the parent theme

And within the “archive.php” file this one:

Overriding the parent theme

At this level let’s once more navigate to the class pages.

Fortunately sufficient, the choose works as anticipated. Good job, everybody!


On this tutorial, we created a WordPress youngster theme and coated the method of sustaining the chosen possibility on web page load. Hopefully, you discovered all of the issues we mentioned right here helpful and also you’ll have the ability to apply them in your upcoming tasks.

Final however not least, all of the recordsdata of our youngster theme can be found as a Github repository.

Extra WordPress Goodness

Supply hyperlink

Spread the love
No Comments

Post A Comment