logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$4
Conditional Statement based on Custom Fields Content

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.

Example:

<?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 : ?>


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.


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



This question has been answered.

West Coast Design Co. | 07/10/11 at 3:48pm Edit


(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/10/11
    3:54pm
    Lee Willis says:

    The check you want is this:

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

  • avatar
    Last edited:
    07/10/11
    3:57pm
    John Cotton says:


    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
    }

    • 07/10/11 4:02pm

      West Coast Design Co. says:

      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.

    • 07/10/11 4:10pm

      John Cotton says:

      not too worried about going back and forth considering everything will be highly cached


      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
      }
      ?>

    • 07/10/11 4:33pm

      West Coast Design Co. says:

      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.

This question has expired.



West Coast Design Co. voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.