jaek Posted March 28, 2012 Share Posted March 28, 2012 I am wanting to echo one of these 4 statements depending on the 'status' value. Currently it is showing the status value on the 2nd line but not the statement below.. <li> <strong>Status: </strong><?php more_fields('status') ?> <?php if (more_fields('status')=="Red") echo "Your account is currently undergoing judgement"; elseif (more_fields('status')=="Green") echo "Your account is currently Live"; elseif (more_fields('status')=="Yellow") echo "Your account is currently undergoing site visits"; elseif (more_fields('status')=="Blue") echo "Your account is currently undergoing insolvency/liquidation";?> </li> any help appreciated. Thanks, Jake Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/ Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Post the code for more_fields() please. Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331896 Share on other sites More sharing options...
jaek Posted March 28, 2012 Author Share Posted March 28, 2012 thanks for the response, the more fields is from a wordpress plugin. All I really know is <?php more_fields('status') ?> shows one of 4 colours selected in wordpress. sorry I can't provide any more information Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331901 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Okay. What specifically does it output on the second line? Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331903 Share on other sites More sharing options...
jaek Posted March 28, 2012 Author Share Posted March 28, 2012 I have a drop down field from a wordpress plugin (morefields) and can select 1 of 4 colours. Red, Blue, Green, Red. The code in the 2nd line shows the selected value as above (1 of the 4 colours) This is part of a wordpress loop if that helps.. Shown below.. <?php if ( is_user_logged_in() ) { $user_id = get_current_user_id(); query_posts( "author=$user_id&posts_per_page=10" );?> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <ul> <li> <strong>Status: </strong><?php more_fields('status') ?> <?php if (more_fields('status')=="Red") echo "Your account is currently undergoing judgement"; elseif (more_fields('status')=="Green") echo "Your account is currently Live"; elseif (more_fields('status')=="Yellow") echo "Your account is currently undergoing site visits"; elseif (more_fields('status')=="Blue") echo "Your account is currently undergoing insolvency/liquidation";?> </li> </ul> </ul> <?php posts_nav_link(); ?> <?php endwhile; else: ?> <p><?php _e('You Currently Have No Debtors'); ?></p> <?php endif; ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331912 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 I think the problem might be that more_fields() is echo'ing the status instead of returning it. So you'll probably want to modify it to be able to return a value. If you find the function definition I can help with that. Alternatively, you can use output buffering as a sort-of hack, but I'd recommend the other option. ob_start(); more_fields('status'); $status = ob_get_clean(); switch($status) { case 'Red' : echo "Your account is currently undergoing judgement"; break; case 'Green' : echo "Your account is currently Live"; break; case 'Yellow' : echo "Your account is currently undergoing site visits"; break; case 'Blue' : echo "Your account is currently undergoing insolvency/liquidation"; break; } Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331915 Share on other sites More sharing options...
jaek Posted March 28, 2012 Author Share Posted March 28, 2012 Nice one! that is now working. Is there any reason this hack is not recommended? as I dont want to waste your time if this is a viable solution The functions I have found are below.. <?php function more_fields($key, $before = '', $after = '', $content_filter = false) { $value = get_meta($key); if (!$value) return false; echo $before; if ($content_filter) echo apply_filters('the_content', $value); else echo $value; echo $after; return true; } /* ** get_box ( ) ** */ function get_box ($name, $args = array()) { global $more_fields; global $post; $html = ''; $nl = "\n"; // Get our fields $more_fields = $more_fields->get_objects(array('_plugin_saved', '_plugin')); // Parse args $defaults = array('format' => 'ul', 'div' => '', 'show' => '', 'title' => ''); $args = wp_parse_args($args, $defaults); if (!is_array($more_fields)) return '<!-- No More Fields are defined -->'; if (!array_key_exists($name, $more_fields)) return "<!-- The box name '$name' does not exist! -->"; // Make sure we've got someting to display $something = false; foreach ((array) $more_fields[$name]['field'] as $field) if (get_post_meta($post->ID, $field['key'], true)) $something = true; if (!$something) return "<!-- Nothing to display for '$name' -->"; // Iterate through our meta fields and generat some html for ($i=0; $i < count($more_fields[$name]['field']); $i++) { $key = $more_fields[$name]['field'][$i]['key']; $title = $more_fields[$name]['field'][$i]['title']; // Set up the list if ($i == 0) { if ($args['div']) $html .= '<div id="' . $args['div'] .'">' . $nl; if ($args['format']) { $caption = ($args['title']) ? ($args['title']) : $name; $html .= '<h3 class="meta_widget_header">' . $caption . '</h3>' . $nl; $html .= '<' . $args['format'] . '>' . $nl; } } // Does this field qualify for being shown? $show = false; if (is_array($args['show'])) { for ($k = 0; $k < count($args['show']); $k++) if ($args['show'][$k] == $key) $show = true; } else if (!$args['show'] || ($args['show'] == $key)) $show = true; $value = get_post_meta($post->ID, $key, true); if ($show && $value) { // Amost the same as 'the_content' filter $value = preg_replace("/\n/", "<br />", $value); $value = wptexturize($value); $value = convert_chars($value); $style_li = ' class="meta_' . $key . '_ul"'; $style_dt = ' class="meta_' . $key . '_dt"'; $style_dd = ' class="meta_' . $key . '_dd"'; if ($args['format'] == 'ul') $html .= "<li ${style_li}>" . $value . '</li>' . $nl; else if ($args['format'] == 'dl') $html .= "<dt ${style_dt}>" . $title . "</dt><dd ${style_dd}>" . $value . '</dd>' . $nl; else if ($args['format'] == 'p') $html .= $value . $nl; else $html .= $value . $nl; } // Close the list and the optional div if ($i == count($more_fields[$name]['field']) - 1) { if ($args['format']) $html .= '</' . $args['format'] . '>' . $nl; if ($args['div']) $html .= '</div>' . $nl; } } echo $html; } /* ** get_meta ( ) ** */ function get_meta ($meta, $id = '') { global $post; if ($id) $meta = get_post_meta($id, $meta, true); else { $id = (get_the_id()) ? get_the_id() : $post->ID; $meta = get_post_meta($id, $meta, true); } return $meta; } function meta ($meta, $id = '') { echo get_meta($meta, $id); } /* ** more_fields_img() ** */ function more_fields_img($meta, $before = '', $after = '', $options = array()) { $defaults = array('height' => 0, 'width' => 0, 'size' => '', 'crop' => false); $options = wp_parse_args( $options, $defaults ); if ( ! ( $id = get_meta($meta) ) ) return false; // If the image size does not exist, make it if ( !$options['size'] && ($options['height'] || $options['width'] ) ) { $size = 'mf_h' . $options['height'] . '_w' . $options['width']; add_image_size( $size, $options['width'], $options['height'], $options['resize'] ); $file = wp_get_attachment_url($id); $file = str_replace(get_option('siteurl'), ABSPATH, $file); $a = image_make_intermediate_size( $file, $options['width'], $options['height'], $options['crop']); $as = explode('/', $file); $original_file = $as[count($as) - 1]; // $b = image_get_intermediate_size($id, $size); //{ $new_file = $a['file']; } // Churn out some HTML $attr = array('class' => 'mf_image attachment-' . $id, 'id' => 'attachment-' . $id); $b = wp_get_attachment_image($id, $size, false, $attr); if ($new_file) $b = str_replace($original_file, $new_file, $b); echo $before. $b . $after; return true; } /* ** more_fields_template_action () ** ** Remplate action to get content of a box. */ function more_fields_template_action ($title, $options = array()) { get_box($title, $options); } add_action('more_fields', 'more_fields_template_action', 10, 2); /* // I'm duplicating this function here - it's in the admin object too. function mf_get_boxes() { global $more_fields_boxes, $more_fields; $more_fields = $more_fields->get_data() ; //get_option('more_fields_boxes'); if (!is_array($more_fields)) $more_fields = array(); if (!is_array($more_fields_boxes)) $more_fields_boxes = array(); foreach (array_keys($more_fields_boxes) as $key) $more_fields[$key] = $more_fields_boxes[$key]; return $more_fields; } */ ?> Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331928 Share on other sites More sharing options...
scootstah Posted March 28, 2012 Share Posted March 28, 2012 Generally you don't want to echo things from a function. As you can see, doing so limits what you can do with it. It looks like modifying that function might mess up things elsewhere. You could add another function to return the output using the hack above. It's not perfect but at least it's less code every time you want to do that. function return_more_fields($key, $before = '', $after = '', $content_filter = false) { ob_start(); if (more_fields($key, $before, $after, $content_filter) === false) { ob_end_clean(); return false; } return ob_get_clean(); } Quote Link to comment https://forums.phpfreaks.com/topic/259874-php-ifshowing-a-different-status/#findComment-1331934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.