• 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….

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter coolstory

    (@coolstory)

    solved:

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

    Thanks for the clue mate.

    I tried yours but not worked. But i just made some modifications in mine and its worked. Here is the code snippet. May be useful for any one else ??

    $posts_array = get_posts( $argse );
    foreach($posts_array as $pst=>$mount)
     {
         $id=$mount->ID;
         echo get_post_meta($id, 'refernce_number', true);
     }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘showing custom fields within get_posts’ is closed to new replies.