While it is possible to use wp_posts table this way, have you considered using a custom field instead? It will give you an easy way to add your YouTube link to your database via your “Add New Post Page” and also allows you to add custom fields programmatically if that is what you need to do.
Additionally, using custom fields allows you to access your custom field in the Loop very easily.
Thanks for the replies cordulack.
I’ve tried using the post meta code, but the results do not match what I expected, because of something or another I can not use the post meta code.
I have tried using the code:
in the “themes\twentyeleven\content.php”
<?php
$youtube = $wpdb->get_results( "SELECT id, name FROM youtube" );
echo "see how it works: "; print_r($youtube);
?>
the result was: see how it works: Array ( )
when i tried using Function Reference using code :
in the “wp-includes\post-template.php”
function youtube_address() {
global $wpdb;
$youtube = $wpdb->get_results( "SELECT * FROM youtube");
print $youtube;
}
and call it using:
see how it works: <?php youtube_address(); ?>
the result was: see how it works: Array
Please cordulack or someone show me whats wrong with my code
this project for my school work.
Thankyou
Using get_results is a little more complicated as it returns an array. So you would have to loop through your results in order to get at the value you want. See here: http://codex.wordpress.org/Class_Reference/wpdb
However, I still would opt for using a custom field.
I would put this in the functions.php file
add_action( 'get_youtube_link', 'output_youtube_custom_field' );
function output_youtube_custom_field() {
$post_id = get_the_ID();
$key = 'youtube_url';
$youtube_url = get_post_meta($post_id, $key, true);
echo $youtube_url;
}
And then put this in the desired place in my theme:
<?php do_action('get_youtube_link'); ?>