0
votes

I use the custom post type "events". I make a custom page template page-events.php.

I make a page "Events" (slug events) as archive page. Select "Events" as page template.

Nothing will show, but when I switch over to default page template insteed of "Events" everythings works fine.

The WP body class shows events-template-default single single-events

So, I don't really why?

My settings:

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 );

?>
1
Hi, Welcome to Stack Overflow! How do you mean 'nothing will show'? Please provide your page-events.php template code so we can see what is in it. Also to create archives for a CPT can be done using the archive template hierarchy instead of needing to make a page in the dashboard and assign a template. - William Patton
I added some code above. I thinks it's a hierarchy problem. The WP body class shows events-template-default single single-events. - ingozoell

1 Answers

0
votes

If you're trying to make an archive page for your post type events, copy stock archive.php of your theme, rename it to archive-events.php and modify it from there to suit your needs.
If you're trying to make a template for a single page of events post type, copy stock single of your theme, rename it to single-events.php and modify it from there.
P.S. You've name your post type events. It's usually a good practice to use singular form instead of plural one, so event in your case

Refer to this image to see how Wordpress selects correct template and this page for hierarchy related information.