Advertisement
vtxyzzy

List Custom Fields in a Row

Mar 13th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. <?php
  2. // Test listing of Custom Fields
  3. $styles = <<<EOSTYLES
  4. <style type="text/css">
  5. .one-car {
  6.    width: 50%;
  7.    margin: auto;
  8. }
  9. .car-attribute {
  10.    width: 30%;
  11.    float: left;
  12. }
  13. .clearboth {
  14.    clear: both;
  15. }
  16. </style>
  17. EOSTYLES;
  18. echo $styles;
  19.  
  20. $attrs = array('Color','Make','Model');
  21. $args = array(
  22.    'cat' => 107,
  23.    'posts_per_page' => 10,
  24.    'ignore_sticky_posts' => 1,
  25. );
  26. query_posts($args);
  27. if (have_posts()) {
  28.    while (have_posts()) {
  29.       the_post();
  30.       $custom_fields = get_post_custom();
  31.       //print_r($custom_fields);
  32.       //break;
  33.       $fields = array();
  34.       $all_cf_keys = array_keys($custom_fields);
  35.       foreach ($attrs as $key) {
  36.          if (in_array($key, $all_cf_keys)) {
  37.             $fields[$key] = $custom_fields[$key][0];
  38.          }
  39.       }
  40.       $cf_keys = array_keys($fields);
  41.       if ($fields) {
  42.          echo "<div class='one-car'>\n";
  43.          foreach ($attrs as $attr) {
  44.             $class = strtolower($attr);
  45.             $value = (in_array($attr,$cf_keys)) ? $fields[$attr] : ' N/A ';
  46.             if(in_array($attr,$cf_keys)) {
  47.                echo "<div class='car-attribute car-$attr'>$value</div>\n";
  48.             }
  49.          }
  50.          echo "</div><!-- /one-car -->\n";
  51.          echo "<div class='clearboth'></div>\n";
  52.       }
  53.    }
  54. }
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement