WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Random Custom field video (4 posts)

  1. keyaspects
    Member
    Posted 2 years ago #

    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>`

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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_posts

  3. keyaspects
    Member
    Posted 2 years ago #

    Hi 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.
    Thanks

  4. keyaspects
    Member
    Posted 2 years ago #

    Hi, 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.

    info / help also gained from here http://webcache.googleusercontent.com/search?q=cache:aTkEUG6KrfkJ:codex.wordpress.org/Class_Reference/WP_Query+display+custom+field+value+wordpress+in+header&cd=2&hl=en&ct=clnk&gl=uk

    <?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(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic