【问题标题】:WordPress Custom Post Type HierarchyWordPress 自定义帖子类型层次结构
【发布时间】: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


【解决方案1】:

如果您要为您的帖子类型 events 创建存档页面,请复制主题的股票 archive.php,将其重命名为 archive-events.php 并从那里修改以满足您的需要。
如果您尝试为events 帖子类型的单页制作模板,请复制主题的库存single,将其重命名为single-events.php 并从那里修改。
附言您已将帖子类型命名为 events。使用单数形式而不是复数形式通常是一个好习惯,所以 event 在你的情况下

请参阅this 图像以了解 Wordpress 如何选择正确的模板和this 页面以获取与层次结构相关的信息。

【讨论】:

  • 谢谢你,你是对的。我也这样做了。但我没有工作。我更喜欢使用自定义模板页面来回显cpt 存档页面,而不是archive-CPT-.php。我可以更好地为 SEO 设置页面(使用 GUI),并将页面选择到 wp 菜单中,而无需为客户进行额外设置。
  • @ingozoell 我刚刚注意到你将has_archive 设置为false so yeah, archive-events.php` 不起作用。 page-events.php template 中的这 3 个点是否有实际代码而不是它们来输出任何内容?
  • 只需the_title()the_content()。但我想知道更多关于 css 主体类的信息:events-template-default single single-events Single,为什么?
  • @ingozoell single 因为它实际上是一个页面,single-events 是因为它是一个带有 slug 的单个页面 events。您的查询是否返回任何结果,是显示问题还是根本没有返回任何结果?
  • 嗯,但是为什么它不会使用现有的single-events.php模板进行显示?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 2018-06-05
  • 2021-05-08
  • 2019-05-03
相关资源
最近更新 更多