在WordPress中的多选框复选框

huangapple go评论69阅读模式
英文:

Multiple checkbox in metabox WordPress

问题

这是你的代码中需要翻译的部分:

//"id" => "value"
$expertise_checkbox_elements = array(
    'design_research'  => pll__('Design research'),
    'product_design' => pll__('Product design'),
    '3d_modelling' => pll__('3D modelling'),
    'engineering' => pll__('Engineering'),
    'dfm' => pll__('Design for manufacturing'),
    'prototyping' => pll__('Prototyping'),
    'production' => pll__('Production')
);

// Save
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    if ( $is_revision || !$is_valid_nonce )
        return; 
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;
    if( ! current_user_can( 'edit_post', $post_id ) )
        return;
    if ( ! empty( $_POST['multval'] ) ) {
        update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
    // Otherwise just delete it if its blank value.
    } else {
        delete_post_meta( $post_id, 'expertise_checkbox_elements' );
    }
}

// Output values
<?php 
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true )); 
foreach ($expertise_elements as $element) {
   echo ('<li>'.$element.'</li>');
}
?>

这里是翻译后的内容:

// "id" => "value"
$expertise_checkbox_elements = array(
    'design_research'  => pll__('Design research'),
    'product_design' => pll__('Product design'),
    '3d_modelling' => pll__('3D modelling'),
    'engineering' => pll__('Engineering'),
    'dfm' => pll__('Design for manufacturing'),
    'prototyping' => pll__('Prototyping'),
    'production' => pll__('Production')
);

// 保存
add_action( 'save_post', 'client_company_save_postdata' );
function client_company_save_postdata( $post_id ) {
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'expertise_checkbox_field_noncename' ] ) && wp_verify_nonce( $_POST[ 'expertise_checkbox_field_noncename' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    if ( $is_revision || !$is_valid_nonce )
        return; 
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return;
    if( ! current_user_can( 'edit_post', $post_id ) )
        return;
    if ( ! empty( $_POST['multval'] ) ) {
        update_post_meta( $post_id, 'expertise_checkbox_elements', $_POST['multval'] );
    // 否则,如果它是空值,就删除它。
    } else {
        delete_post_meta( $post_id, 'expertise_checkbox_elements' );
    }
}

// 输出值
<?php 
$expertise_elements = (get_post_meta( $post->ID, 'expertise_checkbox_elements', true )); 
foreach ($expertise_elements as $element) {
   echo ('<li>'.$element.'</li>');
}
?>

希望这有所帮助!如果你有其他问题或需要进一步的翻译,请随时告诉我。

英文:

I'm making multiple checkboxes in post admin panel. And when user create post and check necessary checkbox it stored it to output in post page.
My code for creating and saving checkboxes.

function expertise_checkbox_meta_box_callback( $post, $meta ){
    $screens = $meta[&#39;args&#39;]; 
    wp_nonce_field( plugin_basename(__FILE__), &#39;expertise_checkbox_field_noncename&#39; );
    $value = maybe_unserialize(get_post_meta( $post-&gt;ID, &#39;expertise_checkbox_elements&#39;, true ));
    //&quot;id&quot; =&gt; &quot;value&quot;
    $expertise_checkbox_elements = array(
        &#39;design_research&#39;  =&gt; pll__(&#39;Design research&#39;),
        &#39;product_design&#39; =&gt; pll__(&#39;Product design&#39;),
        &#39;3d_modelling&#39; =&gt; pll__(&#39;3D modelling&#39;),
        &#39;engineering&#39; =&gt; pll__(&#39;Engineering&#39;),
        &#39;dfm&#39; =&gt; pll__(&#39;Design for manufacturing&#39;),
        &#39;prototyping&#39; =&gt; pll__(&#39;Prototyping&#39;),
        &#39;production&#39; =&gt; pll__(&#39;Production&#39;)
    );
    foreach ( $expertise_checkbox_elements as $id =&gt; $element ) {
        // If the value for checkboxes exist and 
        // this element is part of saved meta check it.
        if ( is_array( $value ) &amp;&amp; in_array( $id, $value ) ) {
            $checked = &#39;checked=&quot;checked&quot;&#39;;
        } else {
            $checked = null;
        }
        ?&gt;
        &lt;p&gt;
            &lt;input type=&quot;checkbox&quot; name=&quot;multval[]&quot; value=&quot;&lt;?php echo $id;?&gt;&quot; &lt;?php echo $checked; ?&gt; /&gt;
            &lt;?php echo $element;?&gt;
            &lt;br&gt;
        &lt;/p&gt;
        &lt;?php
    }
}
##Save
add_action( &#39;save_post&#39;, &#39;client_company_save_postdata&#39; );
function client_company_save_postdata( $post_id ) {
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ &#39;expertise_checkbox_field_noncename&#39; ] ) &amp;&amp; wp_verify_nonce( $_POST[ &#39;expertise_checkbox_field_noncename&#39; ], basename( __FILE__ ) ) ) ? &#39;true&#39; : &#39;false&#39;;
    if ( $is_revision || !$is_valid_nonce )
        return; 
    if ( defined(&#39;DOING_AUTOSAVE&#39;) &amp;&amp; DOING_AUTOSAVE )
        return;
    if( ! current_user_can( &#39;edit_post&#39;, $post_id ) )
        return;
    if ( ! empty( $_POST[&#39;multval&#39;] ) ) {
        update_post_meta( $post_id, &#39;expertise_checkbox_elements&#39;, $_POST[&#39;multval&#39;] );
    // Otherwise just delete it if its blank value.
    } else {
        delete_post_meta( $post_id, &#39;expertise_checkbox_elements&#39; );
    }
}

And this code worked (it's save checkbox status and update it). But when I output array of the checkbox values at post page I see design_research instead of "Design research", dfm instead of "Design for manufacturing" and etc.
Code for output values.

&lt;?php 
$expertise_elements = (get_post_meta( $post-&gt;ID, &#39;expertise_checkbox_elements&#39;, true )); 
foreach ($expertise_elements as $element) {
   echo (&#39;&lt;li&gt;&#39;.$element.&#39;&lt;/li&gt;&#39;);
}
?&gt;

I see that I store "keys" from array, not values. But I can't understand how to make this code output "values" from array.

答案1

得分: 1

您可以将数组的键设置为用户友好的,但更合适的做法是在一个函数中定义选项数组,然后在后端和前端都使用该函数。

英文:

See the answer I provided here:
https://wordpress.stackexchange.com/questions/416533/get-custom-field-label-select-dropdown-on-front-end/416534#416534

> You can set the keys of the array to be user-friendly, but more appropriate would be to define the options array in a function, and then use that function in both the backend and frontend.

答案2

得分: 1

Sure, here is the translated code part:

只需使用最初的数组,作为名称的映射。类似这样:

$names = array(
  'design_research'  => __('Design research'),
  'product_design' => __('Product design'),
  '3d_modelling' => __('3D modelling'),
  'engineering' => __('Engineering'),
  'dfm' => __('Design for manufacturing'),
  'prototyping' => __('Prototyping'),
  'production' => __('Production')
);
$expertise_elements = (get_post_meta($post->ID, 'expertise_checkbox_elements', true));
foreach ($expertise_elements as $el) {
  echo ('<li>' . $names[$el] . '</li>');
}

If you need any further assistance, please let me know.

英文:

Just use the same array you had initially, as a map to names.
Something like this:

$names = array(
  &#39;design_research&#39;  =&gt; __(&#39;Design research&#39;),
  &#39;product_design&#39; =&gt; __(&#39;Product design&#39;),
  &#39;3d_modelling&#39; =&gt; __(&#39;3D modelling&#39;),
  &#39;engineering&#39; =&gt; __(&#39;Engineering&#39;),
  &#39;dfm&#39; =&gt; __(&#39;Design for manufacturing&#39;),
  &#39;prototyping&#39; =&gt; __(&#39;Prototyping&#39;),
  &#39;production&#39; =&gt; __(&#39;Production&#39;)
);
$expertise_elements = (get_post_meta($post-&gt;ID, &#39;expertise_checkbox_elements&#39;, true));
foreach ($expertise_elements as $el) {
  echo (&#39;&lt;li&gt;&#39; . $names[$el] . &#39;&lt;/li&gt;&#39;);
}

Depending on the rest of your code, and to not repeat yourself, you can have that array as class variable, a function or a user-provided list of values/names.

huangapple
  • 本文由 发表于 2023年6月8日 05:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定