Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thanks for the code Jamal. It works great! I made a small adjustment to handle multiple videos and to allow the user to choose which video to display. It wraps them in a div so that they can be styled.

    /* Add youtube_embed shortcode */
    add_shortcode( 'youtube_embed', 'my_youtube_embed_shortcode');
    
    /**
     * New YouTube Embed shortcode.
     *
     * Enables you to insert the New YouTube embed code in a custom
     * field and display it inside your post using the shortcode
     * [youtube_embed]
     * by default it will display all videos added as custom field.
     * The parameter "number" tells it which video to display according to the order it was added to the custom fields.
     * [youtube_embed number="2"] will display only the 2nd "youtube_embed" custom fields.
     */
    
    function my_youtube_embed_shortcode($atts){
    	extract( shortcode_atts( array(
    		'number' => null,
    	), $atts ) );
    
    	global $wp_query;
    
      // Grab the value of youtube_embed meta key
    	$yt_embed = get_post_meta( $wp_query->post->ID, 'youtube_embed', false );
    
    	if ( $yt_embed ) {
    		if ($number == null) {
    			foreach($yt_embed as $code_string) {
    				$embed_code .= '<div class="youtube">' . $code_string . '</div>';
    			}
    		}
    		else {
    			$number -= 1;
    			$embed_code = $yt_embed[$number];
    		}
    	}
    	return $embed_code;
    }
Viewing 1 replies (of 1 total)