Some problems just never go away… especially where CSS3 multi-columns are concerned

Note: This post has been updated.

I don’t use CSS3 multi-columns (based around the column-count and column-gap properties) very much, mainly because a) I don’t need columns in my layouts very often and b) usually when I do need columns, this method is inadequately flexible for what I’m trying to accomplish.

Today I have a good use case though. A simple (but long) list of links, that I want to display as 3 columns on wide screens, 2 columns on tablets and a single column on phones. Great, except when I got it working, I found that — in Chrome only, which is a bit odd — the top of the first column is higher than the rest. It looks fine in Safari and Firefox.

I googled, as always, for a solution, and found several suggestions that seemed like they should work, and people claimed they did work, but for me, they didn’t. Then I found this comment on a post on the topic, and decided to try the column-break-inside property, which is something I normally only use in print CSS.

It still didn’t work. But then, vendor prefix to the rescue! I needed the -webkit prefix, and it worked. This needs to be applied to the elements inside your column container, not the container itself. Here’s my full CSS block:

.columns {
  -moz-column-count: 3;
  -webkit-column-count: 3;
  column-count: 3;
  -moz-column-gap: 3em;
  -webkit-column-gap: 3em;
  column-gap: 3em;
}

/* Fix for unbalanced top alignment in Chrome */
.columns > * {
  -webkit-column-break-inside: avoid;
  column-break-inside: avoid;
}

You’ll also want to repeat the .columns section (or its equivalent for your class/element names) in your media queries, changing column-count appropriately where you want to collapse down to fewer columns.

Since other solutions worked for other people, I’m guessing my solution might not work for everyone either. But I hope it helps someone… maybe you!

Update (6/28/2017)

This problem really does never go away. And I’ve just run into a situation where the above fix doesn’t work. This is only affecting Safari — I think Chrome has actually fixed the bug. But some trial and error led to this new solution that fixes the problem in Safari as well.

/* New fix for Safari */
.columns > * {
  display: inline-block;
  width: 100%;
}

I’m still not totally sure why display: inline-block; is the magic bullet for column alignment issues, but it works. Adding width: 100%; is what keeps these inline blocks from actually appearing on the same line, if they’re short enough. (Also note that I removed the column-break-inside stuff… don’t seem to need it with this change.)

Update (6/12/2020)

Wow… it really never goes away, does it?

Now I’ve found that if I am applying my .columns class to a <ul> tag, its <li> tags lose their bullets, because they apparently need to have display: list-item (or have it implied). When did display: list-item get added to the CSS spec anyway? I’d swear it didn’t even exist when I was first working on this issue, but I’m an old fart now and time has lost all meaning.

Anyway… I did manage to figure out a workaround to this. So add the following after the update above (which you’ll still keep):

/* New fix for Safari */
.columns > li {
  display: list-item;
  margin: 0 !important; /* Maybe you won’t need !important */
  padding-bottom: 0.5em; /* Add some padding to make up for any margin you’re losing above */
}

The Apple Watch review no one was waiting for

Now that I’ve ripped on Apple Music, let’s focus on another Apple product that I’ve recently begun interacting with. Yes, the Apple Watch.

IMG_5153

That’s my orangutan arm, wearing my Apple Watch. It arrived last week. (The watch, that is. I’ve been stuck with the hairy arm all my life.) But let’s not get ahead of ourselves.

Well over a year ago, I joined much of the tech world in engaging in speculation over what we all were, at the time, calling the “iWatch”. (Yes, I correctly deduced that my conviction that Apple wasn’t working on a watch probably meant that they were.) And then, just 8 days shy of a year later, and half a year after Apple did announce their watch, I wrote up some thoughts on who I thought might be the target market for the ludicrously expensive Apple Watch Edition. Turns out I was wrong about where the Edition would be sold (it is, in fact, available at Apple Stores), but I still think I was pretty much on track with my theories about the Edition in general.

Of course, at that point I also mentioned that I wasn’t really in the market for the Apple Watch at all. Here’s what I said:

First off, I personally am not really in the market for an Apple Watch at all. I find it interesting, but a) I don’t really want to wear something on my wrist, and b) I’m not interested in this until the second or third iteration.

It’s true, I didn’t want to wear something on my wrist. I had worn a wristwatch on a daily basis for about 20 years, starting with a supremely geeky Casio calculator watch in the mid-1980s — which would seem to suggest I would be dying for an Apple Watch — but one day in the summer of 2002 I just decided, while heading to the beach on vacation, to take off my watch. I never put it back on.

I had grown accustomed to not wearing a watch, and I liked the feeling. Especially since I had already started carrying a cell phone around in my pocket everywhere I went, it seemed superfluous to strap something on my wrist just to tell the time.

As far as my second reason for not wanting an Apple Watch goes, I was convinced at the time that the first generation Apple Watch would be half-baked upon arrival. But as the reviews started coming in, I got the feeling that while I am sure it will get better over time, it’s actually pretty good already.

As the weeks went by post-release, the voice in the back of my mind started wearing me down, and eventually one day I just became convinced that I should go for it. (OK, honestly? It was a bit of an impulse purchase. Beer may have been involved.)

So it’s decided then… but which model to buy?

There was never any question for me that I would be getting the Apple Watch Sport. Aside from the fact that it is the least expensive model, and the only differences with the more expensive options are aesthetic, I actually like the brushed aluminum look best. As I said in my earlier post, I’m not into shiny metal. And I especially hate gold. (Honestly? I associate it with Donald Trump. In a bad way, if that needs to be said.) Not only that, but I actually like the look of the fluoroelastomer bands the best.

That left me with a couple of choices to consider: whether to buy the 38mm or 42mm size, and then which color band to get. I had the impression, not having worn one, that the larger size might be too big; after all, I don’t have exceptionally large arms or hands. But I checked Apple’s sizing guide and was quickly convinced that, yes, I did need the 42mm version, even though it’s $50 more.

OK. Simple. Color? Not so much. I didn’t really consider the pink or white bands, but the blue, green and black were all in the mix. I decided that I would probably want the black one, just because I don’t exactly want to scream to the world, LOOK AT THIS APPLE WATCH ON MY WRIST! And I felt like the blue or green one would do that.

Except… the model that ships with a black band has “space gray” anodized aluminum, and I just really didn’t like how it looked. My iPhone is “space gray” but I had seen the watches in the display case at an Apple Store, and the “space gray” on the watches is much darker. The one person I know who already owned an Apple Watch also confirmed that for me. I don’t like shiny, but I do like a brushed silver look, so I ended up going with the blue band, and then buying an extra black band. (Apple conveniently offers an option for a black band with the pin in silver instead of “space gray”, perfect for my watch.)

Tick tock, tick tock…

With my selection made and my order placed, it was time for the waiting game. The two items shipped separately, and the black band was actually in stock already, so it arrived almost two weeks before the watch itself. I took the band out of the box and held it up to my wrist, envisioning the watch that it would eventually be connected to.

Apple talks about how “soft” the fluoroelastomer is, and it’s true. I had encountered this kind of rubber before so I knew what to expect, but it really is nice. Considering that you’re paying almost 50 bucks for a couple of strips of rubber with a little bit of aluminum, it should be. And it is.

I checked Apple’s website several times a day for the next week or so, eagerly awaiting the progress of my order. And once it finally did ship, I switched over to the UPS website and watched its progress from Suzhou to Shanghai to Anchorage to Lexington and finally to Minneapolis. Yes! It’s coming!

Unboxing

I was on the phone with a client when the UPS delivery guy walked into the studio carrying a curiously shaped and startlingly heavy box. “You got an Apple Watch, huh?” he said. I suspect this was not the first one he had delivered, although I have yet to see anyone in person besides myself wearing one.

I don’t get into the whole “unboxing experience” the way some people do. The main thing for me is that it’s easy to open. Blister packs are the bane of my existence. Of course this is Apple so opening the Apple Watch box was an event. But it was easy and painless. No blister packs. I felt like the shiny white plastic case it came in was a bit unnecessary, but I can see why they did it. I wonder what the boxes for the stainless steel and Edition models are like. Maybe the box is gold too?

All right, already… do you love it?

You know… yeah, I do. The Apple Watch is a marvelous object. It’s meticulously crafted. It feels like something of quality. The user interface is extremely well designed, and it is serving my main objectives for owning it (which I will outline below) quite nicely. I am very happy to own it.

This device serves a much different purpose than Apple’s other products. It’s not intended to be the next in a line of high-tech toys that we stare at incessantly for hours. Try holding your wrist up and looking at it for more than a few moments and you’ll understand why Dick Tracy isn’t real.

The Apple Watch is… a watch. It’s the watch reimagined, extended. But it still, in spirit, is a watch.

I see the watch as serving three primary roles for me. First, the traditional role of a watch: telling the time, and the day/date while we’re at it, and, being the natural successor to the digital watches of my youth, also offering an alarm, a stopwatch, and a timer. It does all of these watch-like things stunningly well, with the kind of user interface that makes you cringe to think of strapping on a Casio and trying to make sense of those four side buttons. This is Apple doing what it does best: reimagining something we all use and making it better than anyone thought possible.

Second, and really this was the motivating factor for me to get the Apple Watch: fitness tracking. I’ve been running for four years now, and it’s become a big part of my life. I’ve gotten to the point where I generally prefer not to have music while I’m running — mostly because of the annoying tug of earbud cables. I also would really like to not have to carry my iPhone in my pocket, because its weight is another annoyance. (That said, I’ve been inclined to carry my phone with me whenever I run since one time last year — one of the few times I did not have my phone along — when I witnessed a biker crash on the trail in front of me, hard enough to be bloodied and knocked unconscious. Another passerby and I managed to flag down a car whose driver had a phone with him, and we got help for the biker, but if I’d had my phone along the ambulance could have arrived much sooner.)

The Apple Watch works great as a fitness tracker. Granted, I have not used a Fitbit or any other single-function fitness tracking device, but I hate single-function devices, so I love that this capability is built into such a multipurpose object as the Apple Watch. The Apple Watch automatically calibrates using the GPS in your iPhone, so you just need to run for at least 20 minutes with the iPhone initially to let it adjust to your running style. Yesterday I ran without my phone on a route I knew from past experience is exactly 5K (3.1 miles), and the Apple Watch was nearly perfect in measuring the distance.

In addition to workouts, the Apple Watch has configurable tracking that measures your daily activity by three factors: movement (calories burned), exercise (in minutes), and standing — making sure you stand up and move around for at least one minute every hour. It was this feature, and the impact it had on Apple blogger Jim Dalrymple, that really convinced me to check out the Apple Watch in the first place.

I’m already pretty fit from the running, but Activity is still a nice way to make sure I keep moving even on my “rest” days, and especially encouraging me to stand and move around more, because when I’m working I often fall into the habit of sitting at my desk for hours at a time without getting up. (I’ve switched to a standing desk, but I still have a stool that I often rely on when I’m feeling lazy.)

The third thing I was hoping to get from the Apple Watch, and it’s even something Apple promotes about it, is that I would spend less time with my face buried in my iPhone. How does the watch help with this? By providing quick, at-a-glance notifications. The kinds of things that, with a ding and a vibration, have over the past several years been my signal to pull my iPhone from my pocket to review an incoming email… and then check Twitter… and Instagram… and Facebook… and maybe — oh, hang on. My Apple Watch just tapped me on the wrist to remind me to stand up. Have I really been sitting here writing this for so long? BRB.

Ahh… feels good to stretch the legs. Now where was I?

Oh yeah… my iPhone, that constant digital companion, is probably my favorite thing I own, and it shows. Because I spend so much time looking at it. A week into owning my Apple Watch, although I haven’t quantified it, I’m pretty sure I take my iPhone out of my pocket a lot less often now. And I feel liberated from having to answer every incoming message the instant I see it.

So, five out of five stars, then?

I do think I love the Apple Watch. It has taken its place alongside the other Apple devices I use so often. And I pretty much own them all. Mac, iPhone, iPad, I even have an iPod nano that I still use from time to time. My iPhone is definitely my favorite. I’d put the Watch between the Mac and the iPad at this point.

IMG_5164That said, I don’t think it’s absolutely perfect I do wish there were a few more options for the watch face. I’ve tried several of them, and found, in most cases, that they don’t offer as many complications as I’d like, or that the complications are too limited in what they can display. I ended up settling on the “Modular” face, mostly because it is the one that offers the highest information density, and because YES I CAN READ AN ANALOG CLOCK FACE, THANK YOU (and I can still write in cursive too, if I have to) but at a quick glance I prefer a digital display of the time.

The options for complications are a little lacking. What is it with Apple and stocks, for example? iOS and now watchOS just can’t get enough of stocks. I actually am using the Stocks glance now though, since I recently started a Roth IRA. But so far it’s just been a depressing reminder that some money that I’m not going to be able to touch for 30 years is worth a little less now than it was last week. So, who really cares?

I’m also not an astronomer, so while I think the moon phase feature is mildly interesting, it’s not something I need to be able to see at a glance. I really wish you could have the option to make some of the type smaller, since the calendar complication can barely show three words in identifying your next upcoming event.

IMG_5154So my “Modular” face is loaded up with the day/date, weather (in the largest complication to show more info), the sunrise/sunset, activity status, and alarm. I really wish I could fit in one more complication, so I could see the battery level, but in practice that has ended up not being an issue for me at all. I think on my day of heaviest use, the watch still had 15% of its charge at bedtime, and most days it’s still over 30%.

I’m encouraged by the imminent release of watchOS 2.0, and the features it promises, most notably the ability for third-party apps to run directly on the watch. I’ve already decided I do not want to play games on it (I’ve tried a few), and I also am glad there’s no web browser (let’s hope it stays that way), but I do like the potential I see in some of the third-party apps like MLB At Bat, and I’d like to see what else they might be able to do once they’re not using the watch screen as essentially a “dumb terminal” getting data fed to it from the iPhone.

Is that all?

I’ve focused here on the things about the Apple Watch that matter to me. There are several things it can do that I haven’t used (World Clock) or can’t use, at least not yet (anything that involves Apple Watch-to-Apple Watch communication). I’ve sent text messages to my family using the watch, and I’m impressed. The dictation feature shows a marked improvement over the last time I tried to use Siri for that purpose with my iPhone. Even in a noisy environment it seems to work really well.

And I finally got to use Apple Pay with the watch, since my iPhone 5s doesn’t support it directly. I paid for some groceries at Cub Foods the other day, and it was flawless… just double-tapped the bottom button, held my watch up to the reader, and felt the “tap” on my wrist. Done! Well… not quite. Unfortunately, store policy, the cashier then asked to see my ID and credit card, which I had to dig out of my pocket. That pretty much defeats the purpose, but at least I know the technology works.

I also haven’t used any of the music-related functions on the watch. I don’t listen to music from my iPhone very often these days, so having the watch as a remote control for it is unnecessary, and I don’t own Bluetooth headphones, so I can’t test its onboard music player. I swear by my cheap earbuds, so I cringe at the thought of dropping $100 on a pair of Bluetooth earbuds that probably won’t stay in my ears anyway, or might get lost. Honestly, I do not understand why Apple doesn’t allow you to play music through the watch’s built-in speaker. From what I’ve heard so far, it doesn’t sound that much worse than the iPhone’s speaker, and it would be nice to have a little bit of music along with me when I run without needing headphones. (Actually… it just occurred to me that I could try that with my iPhone, but it’s too hard to hear in my shorts pocket while running outdoors. Plus I want to stop carrying my phone when I run anyway.)

So, I’m not using the Apple Watch for everything I could use it for. But for the things I am using it for: checking the time, weather, baseball scores, and shrinkage of my pathetic retirement account; along with tracking my running and other physical activity and just generally avoiding staring at my iPhone so damn much… it’s perfect. And I even like the color.

Why I’ve made the snap judgment that Apple Music is crap

First, let’s begin with several paragraphs of me explaining, in general, why I make snap judgments

As I’ve gotten older, my life seems to be more and more about resisting complexity. I want things to be simple. Part of that comes out of the aesthetics and principles of my job. The web has a tendency towards over-complication, and it’s my job to fight that.

Also, more generally, as you get older there are more and more decisions to be made in a day. Every hour, every minute, every second, you have to make a decision about something. It’s overwhelming and drains your soul. I have to prioritize the things that matter to me, and, more and more, cast the rest aside.

When something new comes along, I have to make a snap judgment. Not necessarily a permanent judgment. That would be foolish and self-defeating. But I do need to make that initial choice: whether or not to let this new thing occupy more of my time right now. Essentially, whether or not to give it a chance.

Sometimes I revisit things I never gave a chance the first time around. On rare occasions I discover that I missed out on something good, and I welcome it in. Most of the time, though, I just confirm that I was right to dismiss it in the first place.

Enter, Apple Music

This week’s biggest choice has been whether or not to embrace Apple Music. On the surface, it sounds like a no-brainer. Apple and Music are two of the biggest parts of my life. I have gulped down my glass of Apple Kool-Aid and asked for a refill, please and thank you. I was a music major in college. I make music, I listen to music. Over a span of 30+ years I have collected and pored over and obsessively thought about music.

Come to think of it, that’s probably the problem.

Apple Music seems like it’s for people who don’t already own a lot of music. I suppose all of the streaming music services are. After all, if you owned it, why would you stream it, other than convenience? But even then, you’d probably just stream it from your iTunes or Amazon library (both of which I have done, often).

From my perspective, owning a music library of over 23,000 songs (enough to play for 75 days, 9 hours and 39 minutes straight, 24 hours a day, without repeating a track), the subscription streaming services have never had much appeal, so I’ve never even tried Spotify, Rdio, etc. But, being an Apple fan, I somehow thought their service might be different. Might be for me. But I guess not.

What would a post like this be without a bullet list?

So what is it about Apple Music that has turned me off? It’s many things, actually:

  • The initial experience of picking, by tapping on hovering bubbles, your favorite genres (from a very narrow and mainstream set), and then favorite artists (from suggestions it pulled in from the selected genres), is basically the same as it was in the old Beats Music service, which I had also already tried and abandoned within 24 hours when it first appeared. This process did quickly home in on many artists that I like, but I reloaded at least a dozen times trying to get it to refine the selections further. Even then, the best it came up with were only what I’d really call “second-tier favorites.” (Also, the UX with the bubbles sucks and needs to be fixed.)
  • Once everything was set up, the “For You” screen did offer me a lot of music I like. But, guess what. Almost all of it was music I already own. And of the 2 or 3 selections it offered that I don’t already own, I was either mildly disinterested or they were musicians I actively loathe.
  • Beats 1. What the hell is this? I mean, OK. The chance that I would actually like what they play on a single, worldwide “radio” station that Jimmy Iovine has anything to do with was already less than zero. But I have actually given it a chance 3 or 4 times, and every time I turn it on, it’s the same “underground” alternative hip hop vibe. This is music I do not dislike. I mean, I wasn’t repulsed by it. It wasn’t Celine Dion or Kenny G. But it’s just not what I’m into, and maybe I just didn’t listen long enough but there was no variety in styles. (Granted, other than the one time I listened to it in the car, I haven’t bothered to leave it on for more than one song. But the first song that’s playing when I turn it on is always in this style.)
  • The return of DRM. I can’t say it any better than this, so I won’t even try.

All of that led me to one simple conclusion, and my snap judgment. I do not want to give Apple Music any more time or attention, at least right now. I just want the same convenient access to my own extensive music library that I’ve come to appreciate with iTunes Match. So I’ve turned off auto-renew on my Apple Music subscription. I’m going to make sure I don’t cancel iTunes Match. And, just to be safe, I’m going to re-download and back up my entire library from iTunes Match just in case… you know… someday.

I love Apple’s hardware and, usually, their OS software. But cripes, they just cannot get online services right, can they?

But see, here’s the thing. Remember how I said I sometimes revisit things I hadn’t given a chance the first time around? I feel like here I am revisiting something. I’ll admit I don’t have extremely vivid or extensive memories of it, since I had only initially looked at it so briefly, but to me Apple Music feels very, very much like Beats Music was. To the point where I find it hard to believe they spent much time at all changing anything about it other than simply fusing it into the iTunes ecosystem. Oh, and adding that pointless radio station.

I may still give it another try at some point before the free trial period ends. Maybe I’ll change my mind. But I doubt it.

Follow the money

One last thought, as I try to make sense of what this is really all about. Apple is a big company, and so are the record labels they had to negotiate with to get permission to offer all of this “content.” My very brief experience listening to music through Apple Music consisted mostly (and, rather strangely, if you think about it) of listening to music that I already had in my library. But because I was listening to these songs through an Apple Music subscription instead of playing them directly from my library, Apple was making micropayments to the record labels for the streams.

So, yes, indirectly, I was paying again for music I already purchased. Just a few fractions of a cent really, but still. That’s the business model here. Especially with the concern I mentioned above (this, if you weren’t paying attention) over Apple Music replacing iTunes Match’s “matched” files with DRM-restricted ones (only if you’ve canceled iTunes Match, apparently), I am left feeling pretty cynical over this whole enterprise, and disappointed that Apple would take things in this direction.

But, then again, they bought Beats, so I shouldn’t be too surprised.

Update: HEY, WAIT! Don’t go. This is important.

I’ve discovered a small change in the new version of iTunes for the Mac (version 12.2) that has a huge impact on all of this. This is the version that introduces Apple Music and changes the icon from red to white.

The ability to tell iTunes to download multiple songs at once is gone. It used to be, if you selected multiple songs in your library — like, all of them — and right-clicked (Ctrl-click), the contextual menu had a Download option, right at the top. Click that, and it starts downloading all of the selected songs.

Um, yeah. That’s gone.

The little icon of the cloud with an arrow is still there. You can click it. You can still download songs. ONE. AT. A. TIME. Good luck with that. So, here’s the important thing: If you haven’t upgraded to iTunes 12.2 yet, DON’T. At least, download all of your music first.

Lucky for me, I have access to multiple Macs, and one of them hadn’t been updated yet. Even as I type this I have my external hard drive hooked up to it, and I’ve begun the process of downloading all 23,000+ songs. Should only take a week or so. (Thanks, CenturyLink!)

In light of the above considerations about money and playing ball with the record labels, I can only interpret the removal of this feature in one way.

Update to the update (August 26, 2015)

Regarding my panic in the update above about the removal of the ability to download “make available offline” multiple tracks at once, I should note that in a subsequent update (not sure which; I’m currently running 12.2.2.25) Apple restored this feature. You can now select multiple tracks, and “Make available offline” appears in the contextual menu. And it works.

I think little by little features are going to get rolled back in that those of us who don’t wholeheartedly embrace streaming services in general, and Apple Music in particular, are asking for. But whether or not those restored features will come with improvements to the interface (especially in the iOS Music app) remains to be seen.

How to REALLY check if the content is empty in WordPress

Problem: You want to check if the content in a WordPress post is empty. Seems easy, but do a Google search on the topic and you’ll see the question asked and — incorrectly — answered several times.

The fact is, I know how to do this. I was just hoping there was a built-in function in WordPress that I didn’t know about. Apparently not, so I wrote my own.

Why isn’t it easy and obvious how to check for the content being empty? Well, you could do this:

if ($post->post_content == '') { ... }

That will work. If the content is really empty. That means a zero-length string. As in strlen($post->post_content) == 0. Which it might be. But probably not.

If you’ve worked with real world site content, or even someone else’s Word documents before, you know that blank space is invisible, and a lot of times there’s a lot of blank space in a document that is not truly “empty.” Spaces, line breaks, HTML paragraphs with nothing but a non-breaking space in them. It all takes up space, and makes the content look empty, even when it’s not.

That last example is the critical one here. A WordPress post may look like it has no content, but if someone pressed Enter while the cursor was in the content box and then saved the page, it most likely has at least one <p>&nbsp;</p> in it.

So what you need is a function that takes all of that invisible cruft into account. Since it doesn’t seem like WordPress has such a function built in, I wrote my own, which I have made as compact as possible:

function empty_content($str) {
    return trim(str_replace('&nbsp;','',strip_tags($str))) == '';
}

This function takes the string you pass into it, strips out all HTML tags, then removes any non-breaking space entities, and then trims all whitespace. If there’s nothing but that stuff, then it becomes an empty string. If there’s any “real” content, the string won’t be empty. Then it just compares whatever it has left against an actual empty string, and returns the boolean result.

So now if you want to check if the WordPress content is really empty, you can do this:

if (empty_content($post->post_content)) { ... }

This will return true if the content is empty; false if it’s not.

Update (8/31/2017): This post continues to be one of the most frequently viewed on my blog, and I’ve been meaning for ages to amend it with this important note. As written here, you’ll get an erroneous true returned if the only content is in HTML, for instance, an image. If you want the function to return false if there’s no text content, then leave it as-is. But you can add a second input parameter to the strip_tags() function to tell it to leave certain HTML tags alone. If you want to allow image tags, for instance, the modified code would read as such:

function empty_content($str) {
    return trim(str_replace('&nbsp;','',strip_tags($str,'<img>'))) == '';
}

How to really exclude the home page in the WordPress wp_page_menu() function

I’m posting this little tip for my future self as much as for anyone else.

The problem: It seems that in recent years WordPress has all-but-deprecated the wp_page_menu() function in favor of the wp_nav_menu() function, but the latter is primarily intended for Custom Menus, and there are some cases where Custom Menus aren’t what you want.

I’m working on a couple of sites right now that have a lot of pages, and I don’t want their editors to have to go over to Appearance > Menus every time they add or move a page. I’m relying on the CMS Tree Page View plugin on these sites to allow the clients to leverage the built in Parent / Order settings on pages to manage their primary navigation, rather than managing a separate Custom Menu.

Since I’m not using a Custom Menu, I don’t want to use wp_nav_menu() at all; I pretty much have to use wp_page_menu(). Which is fine. Except for the fact that the site designs do not call for having Home in the navigation.

No problem, right? After all, I can just add 'show_home' => false to the arguments passed to the function, and it will remove the home page.

But it doesn’t.

I’m not entirely sure what’s going on in the WordPress core here, but I suspect that show_home only works if you’re using the old school default configuration of WordPress that calls for the home page being the main “posts” page. And who does that anymore? If you’ve created a custom home page, even if you’ve configured WordPress properly to display it as the main page, this function doesn’t care. (Do functions “care” about anything? But I digress…)

Remember also that there’s an exclude argument, to which you can pass a comma-delimited string of post IDs to exclude. That works, but… ugh. You mean I really have to hardcode the post ID of the home page right into my header.php file?

Of course not. Use this: get_option('page_on_front') and it will automatically find the home page. This is great if you want to be able to reuse a theme or custom function on multiple sites or just not commit the transgression of hardcoding something that really doesn’t need to be hardcoded.

Here’s the complete code sample. Note there’s no need to bother with show_home at all.

wp_page_menu(array(
    'exclude' => get_option('page_on_front'),
));