【问题标题】:How to echo multiple values from wordpress meta checkbox?如何从 wordpress 元复选框中回显多个值?
【发布时间】:2013-10-12 11:11:30
【问题描述】:

所以我有一个带有复选框的元框,我可以将其用作打开某些内容的开关。 现在它只是回声“OK!”和“不工作......”取决于是否选中了复选框。 我的目标是回应不同价值的不同类型的信息。

例如,其中一间公寓有 Wi-Fi,所以我需要在管理面板中检查“Wi-Fi”,以便在页面上显示 Wi-Fi 图标。

示例: apartments for rent website

他们获得了每个主要功能的图标here

这是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...';
    }
}

我用它在模板中调用它。

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

一切正常,但不是我想要的方式,我想知道如何更改此代码以在页面上回显不同的信息。

例如,我必须在管理面板中选中“第一个标签”以回显页面上的第一张图片。然后我必须在管理面板中检查“第二个标签”以回显第二张图片......等等。但是现在所有这些值都只回显“OK!”和“不工作......”。

【问题讨论】:

    标签: php wordpress checkbox meta-boxes


    【解决方案1】:

    您可以在函数custom_content_all 中构建一个数组,其中包含所有设置的字段。然后返回它。最后检查该字段是否使用in_array 设置为该数组。

    函数如下所示:

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

    你会像这样使用它:

    <?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"; 
    ?>
    

    【讨论】:

    • 旁注:像 Advanced Custom Fields 这样的插件将加快 Meta Boxes 和 Custom Fields 的创建速度。
    • 我已将函数 custom_content_all( $post_id ) 更改为函数 custom_content_all( $id ),现在它运行良好。非常感谢!!!
    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 2023-02-26
    • 2019-01-23
    • 2018-03-04
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多