0

with help from another developer, I have created the following for a custom shortcode:

add_shortcode( 'children' , 'display_custom_post_type_v1' );

function display_custom_post_type_v1($atts) {

$atts = shortcode_atts( array(
    'category' => ''
), $atts );

$categories  = explode(',' , $atts['category']);

$args = array(
        'post_type'     => 'children',
        'post_status'   => 'publish',
        'orderby'       => 'title',
        'order'         => 'ASC',
        'posts_per_page'=> -1,
        'tax_query'     => array( array(
                            'taxonomy'  => 'category',
                            'field'     => 'term_id',
                            'operator' => 'AND',
                            'terms'     => $categories
                        ) )
    );

    $string = '';
    $query = new WP_Query( $args );

    if( ! $query->have_posts() ) {
        return false;
    }

    while( $query->have_posts() ){
        $query->the_post();
        $string .= '<div id="childWrapper"><div id="childImage"><a href="' . get_permalink() . '">' . get_the_post_thumbnail() . '</a></div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
    }
    wp_reset_postdata();

    return $string;
}

So far this works really well for my application, but there is one additional item I would like to include. These custom post types include meta boxes (custom fields) and I would like to include this in the output. Typically I use something like:

 <?php echo get_post_meta( $post->ID, '_cd_birthdate', true ); ?>

But I can't figure out how to include it in the output of the shortcode. I've tried this line, but only the div area shows up, but no content:

 <div>' . get_post_meta( $post->ID, '_cd_birthdate', true ) . '</div>

Any suggestions would be greatly appreciated.

mwgideon
  • 57
  • 2
  • 11

1 Answers1

0

I think '$post->ID,' only works when in the WP loop. Try 'get_the_ID()' instead.

Like...

<div>' . get_post_meta( get_the_ID(), '_cd_birthdate', true ) . '</div>

See this

webguy
  • 664
  • 10
  • 27