我在自定义帖子类型和自定义字段的帮助下创建了一个简单的事件管理插件。在permalink的帮助下,我现在很难在前端显示单个帖子。在shortcode文件中,我列出了所有信息很少的事件(帖子)和一个重定向到帖子指定永久链接的“查看更多”链接。shortcode文件包含以下代码,我想在他们自己的permalink页面中显示每个帖子的所有信息。
<?php
//List events
function et_list_events($atts, $content = null){
$atts = shortcode_atts([
\'title\' => \'My events\',
\'category\' => \'all\',
\'count\' => 2,
\'pagination\' => \'on\',
], $atts);
$pagination = $atts[\'pagination\'] == \'off\' ? false : true;
$paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
//Check the category attributes
if ($atts[\'category\'] == \'all\'){
$terms = \' \';
} else {
$terms = [
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $atts[\'category\']
];
}
//Query Args
$args = [
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'order_by\' => \'start_date\',
\'order\' => \'ASC\',
\'no_found_rows\' => $pagination,
\'post_per_page\' => $atts[\'count\'],
\'paged\' => $paged,
\'tax_query\' => $terms
];
//Get the events from database
$events = new WP_Query($args);
//Check for events
if ($events->have_posts()){
//Get the category slug
$category = str_replace(\'-\', \' \', $atts[\'category\']);
$category = strtolower($category);
$output = \'<div class="event-list">\';
while ($events->have_posts()){
$events->the_post();
$image = wp_get_attachment_image_src(get_post_thumbnail_id(), \'single-post-thumbnail\');
$output .= \'<div class="event-col">\';
$output .= \'<img class="feat-img" src="\'.$image[0].\'" alt="No event featured image is found"/>\';
$output .= \'<h5 class="event-title">\'.get_the_title().\'</h5><br/>\';
$output .= \'<a href="\'.get_permalink().\'">View Details</a>\';
$output .= \'</div>\';
}
$output .= \'</div>\';
//Clear Float
$output .= \'<div style="clear: both;"></div>\';
//Reset Post Data
wp_reset_postdata();
//Pagination
if ($events->max_num_pages > 1 and is_page()){
$output .= \'<nav class="prev-next-posts">\';
$output .= \'<div class="nav-previous">\';
$output .= get_next_posts_link(__(\'<span class="meta-nav">←</span> Previous\'), $events->max_num_pages);
$output .= \'</div>\';
$output .= \'<div class="next-posts-link">\';
$output .= get_previous_posts_link(__(\'<span class="meta-nav">→</span> Next\'));
$output .= \'</div>\';
$output .= \'</nav>\';
}
return $output;
} else {
return \'<p>No events found</p>\';
}
}
add_shortcode(\'events\', \'et_list_events\');
我听说过单页模板,但我没有关于插件的任何信息。谁能告诉我,插件文件夹中应该创建什么文件,应该编写什么代码来显示该帖子的所有字段值。字段值为:
详细信息,开始日期,结束日期,开始时间,结束时间,地点