CMB2 - Wordpress

0

  /**
   * Video metabox
   */

  $videos = new_cmb2_box( array(
    'id'           => $prefix . 'video_url',
    'title'        => 'YouTube Vídeo',
    'object_types' => array( 'video_destaque_home', ), // Post type
    'context'      => 'normal',
    'priority'     => 'high',
    'show_names'   => true, // Show field names on the left
    'cmb_styles'   => true, // false to disable the CMB stylesheet
    'closed'       => false, // true to keep the metabox closed by default
    // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
  ) );

  $videos->add_field( array(
    'name' => 'Endereço do vídeo do YouTube',
    'desc' => 'Insira a url completa de um vídeo do <a href="http://www.youtube.com" target="_blank">YouTube</a>',
    'id'   => $prefix . 'video_url',
    'type' => 'oembed',
  ) );

Good evening!

I installed CMB2 in my theme and following the documentation I was able to create the field to put video in my Custom Post Type, but I am not able to pull the same in the place I want / page etc ...

Can anyone tell me how to pull? Field link created in the post

I tried to pull it like that, but as I said, I'm not aware yet right ...

<?php 
                        $args = array(
                            'post_type' => 'video_destaque_home',
                            'status' => 'publish',
                            'showposts' => 1,
                            'orderby' => 'post_date',
                        );
                        $query = new WP_Query( $args );
                    ?>

                    <?php 
                        // Check that we have query results.
                        if ( $query->have_posts() ) {

                            // Start looping over the query results.
                            while ( $query->have_posts() ) {

                                $query->the_post(); ?>

                                    <div class="qwp-service-video">

                                        <?php get_post_meta($post->ID, 'youtube_video', true ); ?>

                                    </div><!-- /.qwp-service-video -->

                                <?php
                            } 
                        }
                        // Restore original post data.
                        wp_reset_postdata();
                    ?>
    
asked by anonymous 02.05.2017 / 02:48

2 answers

1

Just put the prefix before it's time to call:

get_post_meta($post->ID, '_SEUPREFIXO_youtube_video', true );

The prefix variable was defined there at the beginning of the file. If you have not moved, it is _cmb_ In other words, you would call get_post_meta($post->ID, '_cmb_youtube_video', true );

    
03.05.2017 / 05:37
0

Rolled here guys, thank you to all who helped.

:: FUNCTIONS ::

add_action( 'cmb2_init', 'double_custom_fields' );
function double_custom_fields() {

// Start with an underscore to hide fields from custom fields list


/**
 * Video metabox
 */

$videos = new_cmb2_box( array(
  'id'           => 'video_post_url',
  'title'        => 'YouTube Vídeo',
  'object_types' => array( 'video_destaque_home'), // Post type
  'context'      => 'normal',
  'priority'     => 'high',
  'show_names'   => true, // Show field names on the left
  'cmb_styles'   => true, // false to disable the CMB stylesheet
  'closed'       => false, // true to keep the metabox closed by default
  // 'show_on_cb' => 'yourprefix_show_if_front_page', // function should return a bool value
) );

$videos->add_field( array(
  'name' => 'Endereço do vídeo do YouTube',
  'desc' => 'Insira a url completa de um vídeo do <a href="http://www.youtube.com" target="_blank">YouTube</a>',
  'id' => 'video_url',
  'type' => 'oembed',
  // 'repeatable' => true,
) );


:: FRONT END :: 

<?php 
  $args = array(
    'post_type' => 'video_destaque_home',
    'status' => 'publish',
                'showposts' => 1,
                'orderby' => 'post_date',
  );
  $query = new WP_Query( $args );
?>

<?php 
          // Check that we have query results.
  if ( $query->have_posts() ) {

      // Start looping over the query results.
      while ( $query->have_posts() ) {

          $query->the_post(); ?>

        <div class="recipe-block-video">
                          <div class="block-infos">
                              <?php
              $video_frame = esc_url(get_post_meta(get_the_ID(), 'video_url', true));
            ?>
            <?php echo wp_oembed_get($video_frame); ?>
                          </div>
        </div>

        <?php
      } 
  }
  // Restore original post data.
  wp_reset_postdata();
?>
    
04.05.2017 / 01:12