Random Custom field video
-
Hi,
I’m having trouble getting a random video via a custom field to display in a drop down menu.
I have assigned the following custom field to posts within the ‘videos’ category. ‘recent_video’The custom field contains the url address of the video and nothing else.
The video displays fine if you are on the viewing a post within the videos category or a list of posts from the videos category.
But doesn’t display any other time
here’s the code `<div class=”col_2″>
<?php
$args = array(
‘numberposts’ => 1,
‘category’ => ‘Videos’,
‘post_type’ => ‘recent_video’,
‘orderby’ => ‘rand’
);
$postslist = get_posts( $args );echo ‘<iframe title=”Video” width=”270″ height=”167″ src=”‘ . get_post_meta($post->ID, ‘recent_video’ , true) . ‘” frameborder=”0″ allowfullscreen></iframe>’; ?>
<a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title(); ?>”>Read the post this video came from</a>
<?php wp_reset_query(); ?>
</div>`
-
try:
$postslist = get_posts( $args ); $post_id = $postslist[0]->ID; echo '<iframe title="Video" width="270" height="167" src="' . get_post_meta($post_id, 'recent_video' , true) . '" frameborder="0" allowfullscreen></iframe>'; ?>(untested – generally, you need to get the post ID from the queried post and use that for the custom field)
possibly read http://codex.wordpress.org/Template_Tags/get_postsHi alchymyth,
Thanks for the input. Tried that and now it doesn’t appear at all. hmm.
Bit stumped about this one, will have a read up on the documentation you suggest.
ThanksHi, got it working.
As well as setting the category to an id rather than the name I added in a foreach and setup up the post data which makes it work on all pages.
Thanks again, here’s the code for anyone else that needs it.
<?php $args = array( 'numberposts' => 1, 'category' => 28, 'order' => 'rand' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <?php echo '<iframe title="Video" width="270" height="167" src="' . get_post_meta($post->ID, 'recent_video' , true) . '" frameborder="0" allowfullscreen></iframe>'; ?> <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>">Read the post this video came from</a> <?php endforeach; wp_reset_query(); ?>
The topic ‘Random Custom field video’ is closed to new replies.