【发布时间】:2017-08-23 12:37:25
【问题描述】:
我使用自定义帖子类型“事件”。我制作了一个自定义页面模板page-events.php。
我将页面“事件”(slug events)作为存档页面。
选择“事件”作为页面模板。
什么都不会显示,但是当我切换到默认页面模板而不是“事件”时,一切正常。
WP body 类显示events-template-default single single-events
所以,我真的不知道为什么?
我的设置:
page-events.php
<?php
/*
Template Name: Events
*/
?>
...
<?php
$args = array(
'post_type' => array( 'events' ),
'posts_per_page' => '-1',
'order' => 'ASC'
);
$news_query = new WP_Query( $args );
if ( $news_query->have_posts() ) : ?>
<?php while ( $news_query->have_posts() ) : ?>
<?php $news_query->the_post(); ?>
...
<?php endwhile; endif; wp_reset_postdata(); ?>
functions.php
<?php
// Register Custom Post Type "Events"
function cpt_events() {
$labels = array(
'name' => _x( 'Events', 'Post Type General Name', 'theme_mmm' ),
'singular_name' => _x( 'Events', 'Post Type Singular Name', 'theme_mmm' ),
'menu_name' => __( 'Events', 'theme_mmm' ),
'name_admin_bar' => __( 'Events', 'theme_mmm' ),
'parent_item_colon' => __( 'Events:', 'theme_mmm' ),
'all_items' => __( 'Alle Events', 'theme_mmm' ),
'add_new_item' => __( 'Event hinzufügen', 'theme_mmm' ),
'add_new' => __( 'Event hinzufügen', 'theme_mmm' ),
'new_item' => __( 'Event hinzufügen', 'theme_mmm' ),
'edit_item' => __( 'Event bearbeiten', 'theme_mmm' ),
'update_item' => __( 'Aktualisieren', 'theme_mmm' ),
'view_item' => __( 'Events ansehen', 'theme_mmm' ),
'search_items' => __( 'Suchen', 'theme_mmm' ),
'not_found' => __( 'Keine Treffer', 'theme_mmm' ),
'not_found_in_trash' => __( 'Keine Treffer', 'theme_mmm' ),
);
$args = array(
'label' => __( 'Events', 'theme_mmm' ),
'description' => __( 'Beschreibung', 'theme_mmm' ),
'labels' => $labels,
'supports' => array( 'title', 'editor' ),
'taxonomies' => array(''),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-calendar-alt',
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => true,
'capability_type' => 'page',
);
register_post_type( 'events', $args );
}
add_action( 'init', 'cpt_events', 0 );
?>
【问题讨论】:
-
嗨,欢迎来到 Stack Overflow!你是什么意思'什么都不会显示'?请提供您的
page-events.php模板代码,以便我们查看其中的内容。也可以使用存档模板层次结构为 CPT 创建存档,而无需在仪表板中创建页面并分配模板。 -
我在上面添加了一些代码。我认为这是一个层次结构问题。 WP body 类显示 events-template-default 单个单事件.
标签: wordpress custom-post-type