1

So I've got a meta box with a checkbox that I can use as a switch to turn on certain content. Right now it's echo only "OK!" and "Not working..." depending if a checkbox is checked. My goal is to echo different kind of information from different value.

For example one of the apartment has Wi-Fi in it, so I need to check "Wi-Fi" in admin panel for wi-fi icon to show on page.

Example: apartments for rent website

They got icons for every main feature here

Here's code in functions.php:

$fieldsCheckbox = array(
    'first' => 'First label',
    'second' => 'Second label',
    'third' => 'Third label'
);

add_action("admin_init", "checkbox_init");

function checkbox_init(){
    add_meta_box("checkbox", "Checkbox", "checkbox", "post", "normal", "high");
}

function checkbox(){
    global $post, $fieldsCheckbox;
    $content = '';

    foreach( $fieldsCheckbox as $fieldName => $fieldLabel) {
        $content .= '<label>' . $fieldLabel;
        $checked = get_post_meta($post->ID, $fieldName, true) ? 'checked="checked"' : '';
        $content .= '<input type="checkbox" name="' . $fieldName . '" value=1 '. $checked     .' />';
        $content .= '</label><br />';
    }
    echo $content;
}

// Save Meta 
add_action('save_post', 'save_details');

function save_details(){
    global $post, $fieldsCheckbox;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post->ID;
    }
    foreach( $fieldsCheckbox as $fieldName => $fieldLabel) {
        update_post_meta($post->ID, $fieldName, $_POST[$fieldName]);
    }
}

function custom_content_all($id) {
    global $fieldsCheckbox;

    foreach( $fieldsCheckbox as $fieldName => $fieldLabel ) {
        $fieldValue = get_post_meta($id, $fieldName, true);
        if( !empty($fieldValue) ) {
            echo "OK!";
        }
        else{
            echo 'Not working...';
        }
    }
}

function custom_content_by_name($id, $name) {
    $field_id = get_post_meta($id, $name, true);

    if( !empty($field_id) ) {
        echo "OK!";
    }
    else{
        echo 'Not working...';
    }
}

And I use this to call it inside the template.

<?php custom_content_all(get_the_ID()); ?>

Everything working just fine, but not the way I want it and I want to know how to change this code in order to echo different information on page.

For example I must check 'First label' in admin panel to echo the first picture on page. Then I must check 'Second label' in admin panel to echo second picture...and so on. But right now all this values echo only "OK!" and "Not working...".

4

1 回答 1

1

You can build an array in the function custom_content_all containing all fields that are set. And then return it. And finally check if the field is set it that array using in_array.

The function would look like:

function custom_content_all( $id ) 
{
    global $fieldsCheckbox;
    $the_fields = array();
    foreach( $fieldsCheckbox as $fieldName => $fieldLabel ) 
    {
        $fieldValue = get_post_meta( $id, $fieldName, true );
        if( $fieldValue )
            $the_fields[] = $fieldName;
    }
    return $the_fields;
}

And you use it like:

<?php 
$my_fields = custom_content_all( get_the_ID() ); 
if( in_array( 'first', $my_fields ) ) 
    echo "First";
if( in_array( 'second', $my_fields ) ) 
    echo "Second";
if( in_array( 'third', $my_fields ) ) 
    echo "Third"; 
?>
于 2013-10-12T19:38:34.010 回答