简体   繁体   中英

How to display a list of posts in WP with meta information

In a WP site I'm trying to set up an events page. I've set up a custom post type and defined a metabox using the metabox.io plugin. The metabox contains a date-picker with the id of 'date_1'. I am now attempting to find a way to display a list of posts with the date and title of the event. Title works just fine but I'm having trouble getting the date to display.

The line

$events    .= '<a href="'. get_permalink() .'">' . get_post_meta(get_the_ID(), 'date_1', true) .  " - " . get_the_title() .'</a>';

returns title, but not the date. If I wrap the get_post_meta line in a print_r it returns a 1.

I've also experimented with:

$events    .= '<a href="'. get_permalink() .'">' . get_post_meta($post->ID, $field['date_1'], TRUE ).  " - " . get_the_title() .'</a>';

but this returns "array" instead of one.

Full code

if ( ! function_exists('events_shortcode') ) {

    function events_shortcode() {
        $args   =   array(
                    'post_type'         =>  'kalender',
                    'post_status'       =>  'publish',
                    'order' => 'ASC',
                    'posts_per_page' => 10,
                    );

        $postslist = new WP_Query( $args );
        global $post;

        if ( $postslist->have_posts() ) :
        $events   .= '<div class="events-lists">';

            while ( $postslist->have_posts() ) : $postslist->the_post();
                $events    .= '<div class="items">';
                $events    .= '<a href="'. get_permalink() .'">' . get_post_meta(get_the_ID(), 'date_1', true) .  " - " . get_the_title() .'</a>';
                $events    .= '</div>';            
            endwhile;
            wp_reset_postdata();
            $events  .= '</div>';           
        endif;    
        return $events;
    }
    add_shortcode( 'events', 'events_shortcode' );    
}

Please read metabox.io plugin date field documentation .

I hope you can fix your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM