Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Conditional Statement based on Custom Fields Content WordPress

  • SOLVED

Working on a dynamic page powered by custom fields. In one situation am using CF like categories.

Lets say I have a field called=”Category”

Then entrees such as ”Old Cities” or “New Cities” or “Quite Cities”

I need to display content based on a conditional statement.

<strong>Example:</strong>

<?php if(get_post_meta(‘Category’)(‘Old Cities')):?>

Show Old Cities Information

<?php if(get_post_meta(‘Category’) (‘New Cities)):?>

Show New Cities Information

<?php if(get_post_meta(‘Category’) (‘Quite Cities')):?>

Show Quite Cities Information

<?php : ?>


<em>Your probably thinking am crazy, I know… This has nothing to do with ‘Cities’ this is actually going to be used to display unique Meta Descriptions for SEO landing pages.
</em>

Any suggestions you have would be much appreciated, my PHP skills are extremely limited.



Answers (2)

2011-07-10

Lee Willis answers:

The check you want is this:

if(get_post_meta($post->ID, "Category", TRUE) == 'Old Cities') {
// Do stuff
}

2011-07-10

John Cotton answers:


global $post;

// only do this one - no point in going back to the database time and again for the same thing!
$meta_value = get_post_meta($post->ID, 'Category', true);

switch($meta_value) {
case 'Old Cities':
// Show Old Cities Information
break;
case 'New Cities':
// Show New Cities Information
break;
case 'Quite Cities':
// Show Quite Cities Information
break;
default:
// something to do it none of the above
}


West Coast Design Co. comments:

Thanks John, not too worried about going back and forth considering everything will be highly cached.

The only problem am having is that I lack the proper knowledge about PHP, am always missing a character etc.

In addition, within each ‘case’ there will be more and more echo’s from other fields. Therefore, I need to break up the code.

<?php if ?>

<?endif;?>

Honestly not trying to be lazy, I just spend hours upon hours with this stuff and cant seem to write it properly.


John Cotton comments:

<blockquote>not too worried about going back and forth considering everything will be highly cached</blockquote>

You should be, however cached things are (and unless you've got some fancy stuff running on your DB server, in a high-traffic environment you can't rely on the DB caching in this instance) it's daft to have code that queries the DB multiple times for the same time (ignoring the extra code that the interpreter has to load etc etc).

You don't need if..else, the switch does the work and can be "split up" just as easily.



<?php
global $post;

// only do this one - no point in going back to the database time and again for the same thing!
$meta_value = get_post_meta($post->ID, 'Category', true);

switch($meta_value) {
case 'Old Cities':
// Show Old Cities Information
?>

<!-- HTML HERE -->

<?php
break;
case 'New Cities':
// Show New Cities Information
?>
<!-- HTML HERE -->

<?php
break;
case 'Quite Cities':
// Show Quite Cities Information
?>
<!-- HTML HERE -->

<?php
break;
default:
// something to do it none of the above
?>
<!-- HTML HERE -->
<?php
}
?>


West Coast Design Co. comments:

Thanks John, I don’t expect more then 10 people will ever visit this ‘page’ at the same time. I am using your conditional statement in at least 2 different situations.