Controlling the visibility of content with custom fields
I’ve just finished a project for a client who wanted an interesting feature on his site. He wanted a block of content to be turned on and off whenever he needed it. So when he wanted to promote something, he wanted to add some content and have it display until he was ready to ‘turn it off’. I came up with a unique means of doing this using custom fields. Here’s how you can achieve this:
The Content
Firstly create your page in WordPress that you’ll use as your toggle-able content page. Add your title and content and then add a custom field like active and set the value to yes.
SIDENOTE: It might be a good idea to set the slug of this page to something generic like /info/. This way, if the content has a ‘Read More’ link of sorts, the client wont have to continually change the slug each time he changes the title of this content area.
Now you’re done in the WP Admin, now you can get into your WP PHP template file where this toggle-able content will display.
The Functionality
Basically what you’re going to create, is a code snippet that will query the page ID of the page you created above, but before displaying the content of that page, it’ll check if the custom field value is set to yes. If it’s not, then no content will display, if its set to yes, then… well you can guess what’ll happen
The Code
<? $recentPosts = new WP_Query(); $recentPosts->query('page_id=73'); while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?> <? if (get_post_meta(73, 'active', true) == "yes") { ?> <h3><? the_title(); ?></h3> <? the_content(); ?> <? } else { } ?> <? endwhile; ?>
And that’s that. It’s a pretty simple trick and you should be able to apply the principle to a number of circumstances.
Michael
Great tip Mark!
What if I wanted to switch what you have coded there…
Instead of “yes” to show the content… how could you just “show” content if it has something for that custom field…otherwise show nothing if the key is empty??
I’m thinking it is something along the lines of == “” then show nothing… else…show content of the key — I’m not sure how to change your code to get what I am saying…hopefully it’ll make sense. =)
Thanks in advance,
Mike
MarkB
Heya Michael
Yeah, that’s quite doable.
You’d replace this:
if (get_post_meta(73, ‘active’, true) == “yes”) {
with this:
if (get_post_meta(73, ‘active’, true)) {
You could also switch it around a bit, and say “if its not empty, then continue”… Like so:
if (get_post_meta(73, ‘active’, true) != “”) {
Make sense?
Chris
Hi Michael,
Can you show a working example of this? I tried on the client site you provided but not sure where to look.
Thanks,
Chris
cardgazer.com
Chris
Sorry for the name thing, I saw Michael in the previous comment. Now I know it’s Mark.
Cheers,
Chris
cardgazer.com
MarkB
@Chris – not really sure what I can show you.. It’s more something that is turned on/off in the admin.
On that client example, the contact details at the bottom of the left column is where it’s being used.
On that page in his admin, he can change the custom field value to turn that page’s visibility on/off.
Derek Herman
I’m sorry, but I have to say this makes no sense to me. I make pretty custom WordPress themes and am not seeing the advantages of doing something like this. Where are you adding the custom query too? Is this a separate loop other than the regular loop? Just seems you could add custom content by making a function or plugin that could be reused on all pages and posts and not use a page that has a slug to show snippets. Because Google will index your snippet and link to it and you will have no control over the fact that people can see that page anyhow.
Cherry
People should read this.
krahamath
great thanks.. its very usefull for me and my site also
Игровые Автоматы Онлайн
Отличный сайт для игры онлайн в игровые автоматы.