Basic Custom Fields Technique to Enhance WordPress as a CMS
Author: Ray Gulick; Categories: CSS, Design/Development, PHP for Designers, WordPress; Comments: Be the first!

This is not a new trick, and I certainly did not invent it. It’s been used before, but when I went searching for information about how to do it, it was hard to find. In fact, I finally had to ask a friend who is much better at PHP than I am to help me (I only recently learned to spell P-H-P). This is really basic stuff, but for a lot of us, basic is what we need to get on with our projects.
Sometimes, you need things to NOT appear on a page unless something else that goes with it appears on the page. This is a pretty basic capability for most CMS systems, but I had never done it with WordPress on a page-by-page basis.
Here’s the scenario. I wanted to add a highlighted note in the subnavigation area of my WordPress theme, Evo4 CMS, and I wanted it to be tied to the content of the page it appeared on. I hadn’t used custom fields much, but it seemed to me that was the way to go. Here is an example of the code I wanted to appear below the subnavigation items on the pages:
<div class="sub-note">
<h5>Subnav Note Heading</h5>
<p>Sed do eiusmod tempor incididunt velit esse cillum dolore duis aute irure dolor.</p>
</div>
Pretty simple, right? I decided that, for greater flexibility, my client would have to be responsible for the HTML formatting within the div (this particular client is capable), but I did not want them to be responsible for the div itself, as things can get ugly when someone forgets to close a div or attaches the wrong class to it. In the WordPress backend, on the page on which I wanted the note to appear, I created a custom field named "subnavnote" and added the value (same as above, but without the div):
<h5>Subnav Note Heading</h5>
<p>Sed do eiusmod tempor incididunt velit esse cillum dolore duis aute irure dolor.</p>
On the page template in my theme (page.php), in the subnavigation column, I added the following:
<div class="sub-note">
<?php echo get_post_meta($post->'ID, 'subnavnote', true); ?>
</div>
Woohoo! That displayed the text from the custom field in the subnavigation area. But there was a problem: when I went to other pages, an empty "sub-note" div displayed (CSS specified padding and a background color for the div, which caused it to be visible even when empty). And that was definitely NOT something to yell "woohoo" about.
I needed a way to make the div appear on a page only if there was a value for "subnavnote." No note, no need for the note container. This was where Googling left me high and dry, as I didn’t know the right terms to use, or how to ask the question so that I was able to find a usable answer. I have no doubt the answer was and is out there, but it remained outside my grasp.
After fiddling and failing with some PHP code snippets, I asked a friend who is a PHP coder for help. Here’s the solution she gave me:
<?php $subnavnote_value = get_post_meta($post->ID, 'subnavnote', true); ?>
<?php echo ($subnavnote_value != '') ? "<div class='sub-note'>".$subnavnote_value."</div>" : ''; ?>
Basically, if there is no value for the custom field on a particular page, this code prevents the div that contains the custom field content from appearing. And if there is a value for the custom field, both the value and its container div appear on the page.
I would break down the code and explain it if I was competent to do so, but I’m not (P… uhh… H… um…). I can see how it works, however, and have adapted it to other similar circumstances, which greatly increases my ability to use custom fields. I hope this solution is similarly useful for you.
Keeping Safari (and Chrome) Hacks Out of Your Stylesheets
Author: Ray Gulick; Categories: CSS, Design/Development, PHP for Designers; Comments: 18 Comments

Although I design and develop on a Mac Powerbook, I mostly ignore Safari for two reasons:
- Safari is about as standards compliant as browsers get, so it rarely needs any special attention.
- Safari is used on fewer than 4% of all computers (and it seems to have peaked).
I use Firefox almost exclusively (although I’m now experimenting with Chrome for Mac), because it’s very standards compliant, and it gives me about 99% of the same results as a PC version of Firefox. Using FF in my development process means I have the biggest browser covered (that’s right, FF has over 46% of the market, with IE6-IE8 combining for just over 37%). I use crossbrowsertesting.com to hunt for problems in IE. Ideally, I want my work to look the same in all browsers (but I’ll settle for “close” in IE6).
That said, I try to keep in mind an old saying in the web design business: The most important browser to develop for is the browser on your client’s screen. One of my clients is an ad agency, and they are completely tied to Safari (I’m pretty sure they’ve heard of Firefox, but…). So, looking good in Safari is a priority on their projects. Rarely is there a need for a hack, but occasionally it happens.
There seems to be only one reliable CSS-only way to target Safari (incidentally, it also targets Chrome, which has almost 3x as many users), outlined way back in 2007 by Dustin Brewer. You can put this in your style sheet to enclose Safari-targeted rules:
@media screen and (-webkit-min-device-pixel-ratio:0) {
/* Safari 3.0 and Chrome rules here */
}
But then you have a hack in your style sheet. I find it’s easier to maintain (and find) hacks by putting them into a different file, and your basic style sheet is more likely to validate. I experimented with using the above selector in the header to point to a different stylesheet, but had some issues (don’t remember now what they were). Finally, I settled on putting the following in the header, following the basic style sheet link:
<style type="text/css" media="@media screen and (-webkit-min-device-pixel-ratio:0)">
<!––
<?php include("includes/safariCSS.php"); ?>
––>
</style>
This displays the contents of an include file, displaying CSS rules in the header as an "internal style sheet." The contents of the php include file consists of CSS rules, but is not a CSS document. Internal style sheets in the header are usually to be avoided due to maintenance and consistency issues, but by using an include, the Safari CSS for all pages is editable in one file.
By the way, the CSS rules from the include appear in the header for all browsers, but the “@media…” statement means all but Safari and Chrome ignore the styles. Given that I’ve done websites with some extremely complex CSS and only needed 2-3 rules targeting Safari, I don’t think this approach risks adding much overhead.
View a Demo | Download Demo Files
Server Side Scripting Reminder: Because the include is PHP, which renders on the server, the files will not work locally on your computer. To see the results of your experiments, upload the files to a server that supports PHP.
Quick Tutorial: Simple PHP and CSS to Highlight Current Navigation Section
Author: Ray Gulick; Categories: CSS, Design/Development, Information Architecture, PHP for Designers; Comments: 5 Comments
Good website navigation not only helps visitors find the information they are looking for, it also tells them where they are on a website. This is especially important in light of the fact that people may arrive anywhere on a website via search or a link from another website. They will appreciate some indication of what section of the website they are in; sort of a “you are here” marker.
Essentially, we want to tell the navigation list that, if the navigation section is ‘X’, then this particular navigation list item should have a "current" class applied to it, which would allow us to style that item to highlight the navigation section. But the last thing we need, if we’re busy and have better things to do, is to edit the navigation on each page individually: every time we add or remove a page or change a navigation item name, we would have to update the navigation on every page.
We can make our lives easier with PHP. Remember, PHP executes on the server: you must upload your files to a PHP-enabled server to see it work. It will not execute on your desktop unless you have a local server set up. But if so, you are way beyond this little tutorial…
Below is the HTML for a very simple list-based navigation, and below that, the accompanying CSS. See the demo here»
The demo navigation should look like this:

The HTML
<ul class="navigation">
<li><a href="index.php">Home</a></li>
<li><a href="what.php">What We Do</a></li>
<li><a href="who.php">Who We Are</a></li>
<li><a href="why.php">Why We Do It</a></li>
<li><a href="need.php">Why You Need It</a></li>
<li><a href="contact.php">Contact Us</a></li>
</ul>
The CSS
ul.navigation { margin:0px; padding:0px; list-style-type:none; }
ul.navigation li { display:inline; margin:0; padding:0; }
ul.navigation a { float:left; display:inline; font-size:14px;
font-weight:bold; text-decoration:none; white-space:nowrap; padding:2px 10px 2px 10px; background:#fc0; border-left:1px solid #fff; }
ul.navigation a:link, ul.navigation a:visited { color:#000; }
ul.navigation li a:hover { color:#fff; }
Create a PHP include from the navigation list and place a call for it on each page.
This include is placed on each page in place of the navigation list with the following call for the include:
<?php include("includes/navigation.php"); ?>
This calls for the include, which was named "navigation.php", and placed in a folder called "includes".
Add PHP code to the list items in the include file to display the "current" class when called for:
<ul class="navigation">
<li<?php echo ($navsection == 'home') ? ' class="current";' : "; ?>><a href="index.php">Home</a></li>
<li<?php echo ($navsection == 'what') ? ' class="current";' : "; ?>><a href="what.php">What We Do</a></li>
<li<?php echo ($navsection == 'who') ? ' class="current";' : "; ?>><a href="who.php">Who We Are</a></li>
<li<?php echo ($navsection == 'why') ? ' class="current";' : "; ?>><a href="why.php">Why We Do It</a></li>
<li<?php echo ($navsection == 'need') ? ' class="current";' : "; ?>><a href="need.php">Why You Need It</a></li>
<li<?php echo ($navsection == 'contact') ? ' class="current";' : "; ?>><a href="contact.php">Contact Us</a></li>
</ul>
Add styles to highlight the navigation items when list item has "current" class applied.
ul.navigation li.current a:link, ul.navigation li.current a:visited
{ color:#fff; background:#000; }
ul.navigation li.current a:hover { color:#fff; background:#c00; }
One last thing: put the appropriate navigation section info on each page.
At the very top of the HTML page, prior to the doc statement, add the following 3 lines of code:
<?php
$navsection = 'home';
?>
Note that this particular example would be found above the doc statement on the home page. Because it matches the navsection statement attached to the home page list item on the menu, that list item displays with the "current" class.
This technique works well on the top level of list-based drop-down menus as well, though I have found that it’s easier to attach the "current" class to the link rather than to the list item. CSS, of course, must be adjusted accordingly.


Recent Comments