Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to get a video to display inside a widget which is called from a custom field inside a post. The code I am using is here:

<?php query_posts('cat=8&showposts=1'.get_option('posts_per_page')); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?><?php if( get_post_meta($post->ID, "youtube", true) ): ?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="The W Club: <?php the_title(); ?>"><?php the_title(); ?></a></h2>

<iframe width="250" height="199" src="http://www.youtube.com/embed/<?php echo get_post_meta($post->ID, "youtube", true); ?>?rel=0" frameborder="0" allowfullscreen></iframe>

<?php else: ?>

Video can not be displayed.

<?php endif; ?>

<?php endwhile; endif; ?>

<?php echo get_post_meta($post->ID, "youtube", true); ?>    

I have created a post inside category 8 (videos), entered a title, added the custom field (youtube) with the right code for the URL to display the video.

When I load the page all the widget displays is "Video can not be displayed", it doesn't use the post title and I can't get it to even echo the custom field entry.

Any ideas as to what is going on? The custom php widget I am using is http://wordpress.org/extend/plugins/php-code-widget/

Thanks in advance.

Of course if anyone has a better way of doing what I'm trying to achieve that would be awesome too :)

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

query_posts is for modifying the main loop only, if you're doing additional queries outside of or within the main loop, create a new WP_Query instance.

also- I'm not sure why you have showposts=1 and then get_option('posts_per_page') immediately after. If for example posts_per_page was set to 10, that string would evaluate to showposts=110

share|improve this answer
    
Sorry, I don't really understand what I should do? I'm not a php coder. I have removed 'get_option('posts_per_page') now. The problem seems to lie with the custom field call, any ideas what I can do to fix this? –  Dave Burns Jan 15 '12 at 16:41
add comment

I didn't undertand you well but if you are saying that you save a custom field in a specific post you could use get_post()

<?php $youtube_post = get_post($my_id); ?>

<h2><a href="<?php echo get_permalink( $youtube_post->ID ); ?>" rel="bookmark" title="The W Club: <?php echo get_the_title($youtube_post->ID); ?>"><?php echo get_the_title($youtube_post->ID); ?></a></h2>

<iframe width="250" height="199" src="http://www.youtube.com/embed/<?php echo get_post_meta($youtube_post->ID, "youtube", true); ?>?rel=0" frameborder="0" allowfullscreen></iframe>

Just replace $my_id with the post ID that you can retrieve, however if you just show a static video the best solution will be using a custom Video Widget where you can put the title and the video url, and the WP Embed class would do the work.

share|improve this answer
add comment

I have solved it using the following code, thanks to @Milo for the help in locating the issue and also an online guide: http://justintadlock.com/archives/2008/01/25/how-to-add-videos-to-your-wordpress-sidebar

<?php $my_query = new WP_Query('cat=8&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post(); 

$video = get_post_custom_values($key = 'youtube');?>

<?php if(isset($video[0]) && strcmp($video[0],'')!= 0)  {?>

<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="The W Club: <?php the_title(); ?>"><?php the_title(); ?></a></h2>


<iframe width="250" height="199" src="http://www.youtube.com/embed/<?php echo $video[0]; ?>?rel=0" frameborder="0" allowfullscreen></iframe>


<?php } else {
echo 'Did not add a video URL to the custom field <strong> Key</strong> of "Video"';
echo '<!-- This user did not add the video URL to the correct custom field -->';
} 
?>

<?php endwhile; ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.