A little while ago, while working on a site built by another developer, I came across this rather interesting example of how to use custom fields badly in a WordPress theme (abbreviated for, well, brevity):
(The original also did this for meta keywords and meta descriptions — but the demonstration of this “logic” only requires one field.)
if (is_front_page()) { ?>
Handwritten title
} elseif (is_page("page-name")) { ?>
= get_post_meta(334, 'meta_title', TRUE); ?> | bloginfo('name'); ?>
} elseif (is_page("page-name-2")) { ?>
= get_post_meta(383, 'meta_title', TRUE); ?> | bloginfo('name'); ?>
} elseif (is_page("page-name-3")) { ?>
= get_post_meta(381, 'meta_title', TRUE); ?> | bloginfo('name'); ?>
} elseif (is_page("page-name-4")) { ?>
= get_post_meta(383, 'meta_title', TRUE); ?> | bloginfo('name'); ?>
} elseif (is_page("page-name-5")) { ?>
= get_post_meta(387, 'meta_title', TRUE); ?> | bloginfo('name'); ?>
} ?>
And so on. For approximately 40 separate pages. It made my brain hurt. For reference, the exact same thing — for all pages on the site — could have been accomplished (with better fallback conditions, in fact) with this code:
post->ID, 'meta_title', true)=="" && is_page() ) { ?>
wp_title('|', true, 'right'); ?> bloginfo('name'); ?>
post->ID, 'meta_title', true)); ?> | bloginfo('name'); ?>
Now, the original code may actually look cleaner — it does, after all, have fewer functions and fewer variables. However, the second example is a hell of a lot more maintainable.
If you add a new page to the site in the first example, you have to:
- Create the new page.
- Add a custom field with the title.
- Check the new page’s ID.
- Find the theme file which contains the meta data references.
- Add a new line in the
elseif
loops which references your new page first by slug and then by ID
With the second example, you simply:
- Create the new page.
- Add a custom field with the title.
No coding, no PHP (Hypertext PreProcessing), no editing themes — it just works. Well, isn’t that handy? This is just basic good coding practice: make your code reusable. There’s absolutely no reason to code something into your WordPress Themes which is not readily transportable unless you’re doing yourself a favor by avoiding an unnecessary server call by hard-coding the site name or other known elements.
The basic difference between these two examples is simple: the first requires you to hard code the ID and page slug for each example; the second grabs the post ID from the existing post object. The second example also has a fall-back if no information has been entered in a given custom field — which is lacking in the original code.
Word to the wise: save yourself some work!
Mildura
; July 8, 2010 at 7:35 amLOL I have seen that done before when i outsourced a job… it was for a joomla template and they were using the exact same method to output different templates, on a per page basis. god dam i thought it was bad :p
מאמרים
; April 24, 2010 at 1:07 am40 pages is not that bad 🙂
Joe Dolson
; March 21, 2010 at 1:19 pmSeriously? You think that 40 pages is an exaggeration?
MaybeTheTruth
; March 21, 2010 at 11:28 am“And so on. For approximately 40 separate pages. ”
Ah, the coder’s exaggeration. I love it. Every time. “OMG I had to change about three hundred meta tags!” or “I can’t believed they used NINETY nested loops.”
Its always only three or four.
Poker
; August 29, 2009 at 9:03 amThanks for giving the suggession to us. The meta fields has to be posted correctly in the wordpress themes. Till now I don’t know how to add the meta fields to the wordpress theme. From the information you had given i come to know that how to post the meta fields in it.