简体   繁体   English

如何在 WP 中显示带有元信息的帖子列表

[英]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.在 WP 站点中,我试图设置一个事件页面。 I've set up a custom post type and defined a metabox using the metabox.io plugin.我已经设置了自定义帖子类型并使用 metabox.io 插件定义了一个元框。 The metabox contains a date-picker with the id of 'date_1'. metabox 包含一个 ID 为“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.如果我将 get_post_meta 行包装在 print_r 中,它将返回 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 .请阅读 metabox.io 插件日期字段文档

I hope you can fix your problem我希望你能解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WP 如何在一个类别的管理面板中显示一个包含该类别所有已分配帖子列表的字段? - WP How to display a field with a list of all assigned posts of this category in the admin panel of a category? Wordpress:如何按元值按降序从 wp_posts 和 wp_postmeta 获取记录? - Wordpress: How to fetch record from wp_posts and wp_postmeta in desc order by meta_value? 如何通过多个meta键在wordpress中显示热门帖子? - how to display popular posts by multiple meta keys in wordpress? 显示元字段 PHP WP - Display Meta Fields PHP WP WP短代码以按ID显示帖子 - WP Shortcode to display posts by ID 使用WP类别帖子列表小部件动态显示特定类别(WP / PHP) - Using WP Category Posts List Widget To Dynamically Display Specific Category (WP/PHP) 显示来自 WP Query 的帖子和每 4 个帖子,显示 4 个带有分类的帖子 - Display posts from WP Query and every 4 posts, display 4 posts with taxonomy 如何在 WP_Query 中显示所有帖子以及元查询? - How can I show all posts along with meta query in WP_Query? WP JSON REST API(Ryan McCue)如何使用“ AND”关系查询具有特定元数据的帖子? - WP JSON REST API (Ryan McCue) how to query posts with specific meta data with 'AND' relation? 如何列出带有相关图像的wp帖子? - How do I list wp posts with releated images?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM