如果选中自定义元复选框,则显示内容

时间:2017-08-15 作者:s_danielle

我有一个自定义元框在我的页面管理与5个复选框。如果选中一个或多个复选框,我希望页面上显示某些内容。

我已经在管理员中正确显示并保存了元框复选框,但是我需要在模板文件中放置什么来显示基于是否从管理员选中复选框的内容呢?

例如,如果在admin中选中了Web开发、徽标设计和打印设计,那么我希望页面显示以下内容:

网页开发

  • 标志设计
  • 印刷设计
    • 我的职能如下。php文件:

      add_action( \'add_meta_boxes\', \'add_custom_box\' );
      
      function add_custom_box( $post ) {
          add_meta_box(
              \'Meta Box\', // ID, should be a string.
              \'Services\', // Meta Box Title.
              \'services_meta_box\', // Your call back function, this is where your form field will go.
              \'page\', // The post type you want this to show up on, can be post, page, or custom post type.
              \'normal\', // The placement of your meta box, can be normal or side.
              \'core\' // The priority in which this will be displayed.
          );
      }
      
      function services_meta_box($post) {
      wp_nonce_field( \'my_awesome_nonce\', \'awesome_nonce\' );    
      $checkboxMeta = get_post_meta( $post->ID );
      ?>
      
      <input type="checkbox" name="ui-ux-design" id="ui-ux-design" value="yes" <?php if ( isset ( $checkboxMeta[\'ui-ux-design\'] ) ) checked( $checkboxMeta[\'ui-ux-design\'][0], \'yes\' ); ?> />UI/UX Design<br />
      <input type="checkbox" name="web-development" id="web-development" value="yes" <?php if ( isset ( $checkboxMeta[\'web-development\'] ) ) checked( $checkboxMeta[\'web-development\'][0], \'yes\' ); ?> />Web Development<br />
      <input type="checkbox" name="logo-design" id="logo-design" value="yes" <?php if ( isset ( $checkboxMeta[\'logo-design\'] ) ) checked( $checkboxMeta[\'logo-design\'][0], \'yes\' ); ?> />Logo Design<br />
      <input type="checkbox" name="branding" id="branding" value="yes" <?php if ( isset ( $checkboxMeta[\'branding\'] ) ) checked( $checkboxMeta[\'branding\'][0], \'yes\' ); ?> />Branding<br />
      <input type="checkbox" name="print-design" id="print-design" value="yes" <?php if ( isset ( $checkboxMeta[\'print-design\'] ) ) checked( $checkboxMeta[\'print-design\'][0], \'yes\' ); ?> />Print Design<br />
      
      <?php }
      
      add_action( \'save_post\', \'save_services_checkboxes\' );
      function save_services_checkboxes( $post_id ) {
          if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
              return;
          if ( ( isset ( $_POST[\'my_awesome_nonce\'] ) ) && ( ! wp_verify_nonce( $_POST[\'my_awesome_nonce\'], plugin_basename( __FILE__ ) ) ) )
              return;
          if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
              if ( ! current_user_can( \'edit_page\', $post_id ) ) {
                  return;
              }    
          } else {
              if ( ! current_user_can( \'edit_post\', $post_id ) ) {
                  return;
              }
          }
      
          //saves ui-ux-design\'s value
          if( isset( $_POST[ \'ui-ux-design\' ] ) ) {
              update_post_meta( $post_id, \'ui-ux-design\', \'yes\' );
          } else {
              update_post_meta( $post_id, \'ui-ux-design\', \'no\' );
          }
      
          //saves web-development\'s value
          if( isset( $_POST[ \'web-development\' ] ) ) {
              update_post_meta( $post_id, \'web-development\', \'yes\' );
          } else {
              update_post_meta( $post_id, \'web-development\', \'no\' );
          }
      
          //saves logo-design\'s value
          if( isset( $_POST[ \'logo-design\' ] ) ) {
              update_post_meta( $post_id, \'logo-design\', \'yes\' );
          } else {
              update_post_meta( $post_id, \'logo-design\', \'no\' );
          }  
          //saves branding\'s value
          if( isset( $_POST[ \'branding\' ] ) ) {
              update_post_meta( $post_id, \'branding\', \'yes\' );
          } else {
              update_post_meta( $post_id, \'branding\', \'no\' );
          }  
          //saves print design\'s value
          if( isset( $_POST[ \'print-design\' ] ) ) {
              update_post_meta( $post_id, \'print-design\', \'yes\' );
          } else {
              update_post_meta( $post_id, \'print-design\', \'no\' );
          }  
      }
      

    2 个回复
    最合适的回答,由SO网友:inarilo 整理而成

    使用get_post_meta 获取每个值并与“是”进行比较。E、 g。

    if(get_post_meta($post->ID, \'ui-ux-design\', true) == \'yes\') {
      //show relevant content
    }
    

    SO网友:Devin Price
    if ( \'yes\' == get_post_meta( get_the_ID(), \'ui-ux-design\' ) ) :
        // Display your content
    endif;
    

    Codex: get_post_meta()

    结束

    相关推荐

    CPT GROUP BY DATE Metabox值

    我正在使用自定义帖子类型构建一个事件插件。我运行了一个循环,它将使用一个短代码显示页面上的所有事件。我的问题是如何设置它,以便它按元数据库中设置的月份对事件进行分组_eDate 并将该组放在一个标题下。例如,7月的所有事件将显示在7月h1标记下。以下是我当前使用的循环:function events_list_shortcode() { // Query Post Type $args = array( \'post_type\' =>