Columns

Faux Mega Menu in WordPress

Sometimes you have so many items in your WordPress blog’s navigation that you feel that it’s far too long. But if they are in the proper logical division splitting them into other menus is not be an option.

There are fantastic tools to create “Mega Menus” with jQuery and CSS such as this one. But on the standard WordPress menu you find in themes like 2010 or Thematic, implementing can be a challenge sometimes.

Luckily for us we can use CSS3 for a quick fix to make your drop down menu split itself into columns. The most obvious answer is to use the new “Columns” property but unfortunately it is not widely supported, even among modern browsers.

For reference, in this example I am styling a drop down menu in Thematic.

.sub-menu{display:inline-block; width:500px !important;}

.sub-menu li{width:50% !important;}

.sub-menu li a:hover{color:#FFFFFF;}

.sub-menu li:nth-child(even){float:right;}

All we are telling it to do is to make the sub-menu 500px wide, make every list item 50% of that, and using the nth-child property, tell every even-numbered element to float right.

Making it 3 columns would be as easy as dropping the li width to 33% if we wanted. Applying styles is easy as well. If we wanted a border dividing the left and right column (if mine didn’t already have borders) we could further edit the nth-child(even) and give it a left border, or make a new rule for nth-child(odd) and give it a right border.

Super easy!

Widgetizing the Footer

Widgetizing the Footer

I really like this theme for WordPress and wanted something not so graphically intensive as Behemoth Gaming. However I wanted to add some widgets to the footer and this theme by default does not have a widgetized section.

Luckily for us that is easily solved. So how do you add a widget-ready section to your WordPress theme? Let’s show you how to do it!

The first thing I would recommend is make a child theme so that if your original theme gets updated your changes are not lost. But we aren’t covering that in this post so we will assume you either have a child theme or are confident you won’t overwrite your changes.

Using your choice of PHP editor (you can also easily do this from the Appearance Editor menu in WordPress if you wish) navigate to your functions.php file. What you are looking for in this file is some code that looks like “register_sidebar”. This tells your WordPress them what widget sections you have. In this theme the code I was looking for looked like this:

register_sidebar(array(
'id' => 'right-sidebar',
'name' => 'Right Sidebar',
'before_widget' => '<aside>',
'after_widget' => '</aside>',
'before_title' => '<h3>',
'after_title' => "</h3>",
));

This adds my sidebar so I can use the handy drag-and-drop widgets. In that same block of code (actually I did mine in my child theme but either way) I added this chunk of code which is very similar:

register_sidebar(array(
'name' => 'Footer',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));

We have a couple options here. First is the name, which is the name of the widget section that we will be adding to the rest of our code in a minute. When you look to add widgets to this section, this is the name of the container as you drag them around.

The before_widget is any chunk of HTML you would like to be placed before your widget. As you can see, this is usually some opening tag but can also include anything you want. Want an image before every widget? You can do that too.

The other three options should be self-explanatory as well. These give you fairly robust control over the appearance of your widgets.

Now that we have defined the style and name of our widgetized section, we actually need to add the location for it to live within our template files. Within my footer.php I added the code:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Footer") ) : ?>
<?php endif; ?>

Widgetizing the Footer

Change the word “Footer” to whatever you named your new section in your functions.php file and save it. Provided all the code is correct you can now browse over to your Widgets menu and start adding!

Let me know if you have any problems or corrections!

Fake Conditional Statements in CSS

For a first post, let’s start this off simple. I saw a question on Reddit the other day asking if you could dynamically change the size of elements depending on what the user clicked on without using Javascript. [http://goo.gl/SErS9]

You can almost create this effect with CSS3 selectors and no Javascript at all.
Here is the CSS:

#div1, #div2
{
width:200px;
border:solid 1px #000000;
float:left;
margin-right:10px;
height:50px;
overflow:hidden;
}
div[id^="div"]:hover
{
height:200px !important;
}
div:target
{
height:200px !important;
}

And the HTML:

<a href="conditional-test.html#div1">Link to Div1</a>

<a href="conditional-test.html#div2">Link to Div2</a>
<div id="div1">

<a name="div1">This is a bunch of data.</a>
This is more data you can't see yet.

</div>
<div id="div2">

<a name="div2">This is a bunch of other data.</a>
This is more data you can't see yet.

</div>

The first handy thing to point is the chunk of CSS that says div[id^=”div]

At first glace this might seem like unnecessary mark-up, but the carot symbol (^) is actually a CSS identifier that says “target anything that starts with.”

So in this case, I am looking to target divs ( div[id^=”div] ) whose ID starts with ( div[id^=“div] ) the word “div.” ( div[id^=”div] )

As you can see in the HTML I have to divs names “div1” and “div2” and this was just one way to target both of them. This becomes more effective the more elements you have that have similar names and share similar styles, but have their own variations or perhaps are dynamically generated by something like a C# control and they end up with weird additions to what were your nice cleanly defined classes.

At the top of the HTML, you can see that we have two links, each ending in a hash tag and the name of one of our divs. When these links are clicked on, that name is appended to the end of the URL and is referred to as the “target.”

This incorporates the final chunk of our CSS that pretty much says if there is a div on the page with the same name as this target in the URL, append this style which in this case happens to make the div tall enough to see the other sentence of content.

With this method you can’t do fun things like animating the application of styles, but the technique does work and is super simple to do if you want to implement it in a hurry.