Forums

showing custom fields within get_posts (2 posts)

  1. coolstory
    Member
    Posted 9 months ago #

    I am desperate... About to start crying :(

    I went through google and word press function reference pages several times, but still cant solve one simple question:

    how to show custom fields if I have the following code (working through shortcode though):

    function slider($atts) {
    	extract(shortcode_atts(array(
    		'category' => ''
    	), $atts));
    ob_start();
    global $post;
    $args = array('category' => $category, 'numberposts'=>3, 'order'=>ASC);
    $myposts = get_posts( $args );
    echo'
    <div class="flexslider">
    <ul class="slides">
    ';
    foreach( $myposts as $post ) : setup_postdata($post); 
    
    ?>
    <li>
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?>
    <p class="flex-caption"><?php $id=the_id(); echo get_post_meta($id, 'caption', true);?></p></a>
    </li>
    	<?php
    endforeach;
    echo'</ul></div>';
    	$content = ob_get_contents();
    	ob_end_clean();
    	return $content;
    }
    
    add_shortcode('slider', 'slider');

    As u can probably see, Im trying to get one custom field for each post in the loop using:

    <?php $id=the_id(); echo get_post_meta($id, 'caption', true);?>

    But I get just ID numbers of posts. But if I use just

    <?php echo get_post_meta(666, 'caption', true);?>

    I manually insert some posts ID then I get its custom field "caption".

    I simply dont understand whats going on....

  2. coolstory
    Member
    Posted 9 months ago #

    solved:

    <p class="flex-caption"><?php global $post; $thePostID = $post->ID; echo get_post_meta($thePostID, 'caption', true);?>

Reply

You must log in to post.

About this Topic