CMB2 - Wordpress

Asked

Viewed 147 times

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 night!

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’m not able to pull the same in the place I want/page etc...

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

I tried to pull like that, but like I said, I’m not quite sure yet...

<?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();
                    ?>
  • how is your loop?

  • In fact, really, I haven’t done anything yet loop or anything on the call, because I have no idea what to call when it’s a custom field. A friend told me, which is as follows <? php $image = get_post_meta($post->ID, Imagem', true); ? > ?

  • if this plugin saves this box as metadata (which I believe it to be), this may be a way

2 answers

1

Just put the prefix before the call:

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

The prefix variable was set there at the beginning of the file. If you haven’t touched it, it is _cmb_ Or so you would call it get_post_meta($post->ID, '_cmb_youtube_video', true );

  • I tried to do it that way, even though it didn’t happen yet, the fuck is that as I have no experience with programming and WP, I stay in the dark without knowing the reason hehe, fuck. cmb and yet it hasn’t rolled yet...

  • Something tells me this video will help me https://www.youtube.com/watch?v=-FVX5RZ4F6k !

0

It rolled here guys, thank you very much 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();
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.