2017-06-06 94 views
0

我在wordpress中的函数“register_post_type”有问题。wordpress中的register_post_type没有工作模板

我在functions.php中添加代码(主题路径):

add_action('init', 'events'); 

function events() { 
    register_post_type('events', array(
    'labels' => array(
     'name' => 'Wydarzenia', 
     'singular_name' => 'events', 
    ), 
    'description' => 'Wydarzenia', 
    'public' => true, 
    'menu_position' => 20, 
    'supports' => array('title', 'editor', 'custom-fields', 'thumbnail', 'excerpt') 
)); 
} 

,然后加模板:单events.php和归档events.php但这没有工作......每次显示:指数.PHP ..

single.php中没有工作的事件......

在哪里的问题?谢谢!

+0

'rewrite'=> array('slug'=> events')add slug –

回答

2

我认为你缺少page-events.php

创建后类型events后。在你的WP管理员,创建一个名为events页,然后在代码中创建一个页面page-events.php,然后证明,u在乌尔自定义后类型创建的所有事件的上市events

<?php 
$args = array 
(
    'post_type' => 'events', 
    'posts_per_page' => 10 
); 
$events_query = new WP_Query($args);?> 
<?php if($events_query->have_posts()):?> 
    <ul> 
     <?php while($events_query->have_posts()): $events_query->the_post();?> 
      <li> 
       <a href="<?php the_permalink();?>"><?php the_title();?></a> 
      </li> 
     <?php endwhile; wp_reset_postdata(); ?> 
    </ul> 
<?php endif;?> 

,那么你可以创建一个single-events.php到显示单个事件的内容。

1

add_action('init','events');

功能的事件(){

register_post_type( '事件',阵列(

'labels' => array(

    'name' => 'Wydarzenia', 

    'singular_name' => 'events', 

), 

'description' => 'Wydarzenia', 

'public' => true, 

'menu_position' => 20, 

'has_archive' => true, 

'rewrite' => array('slug' => events'), 

'supports' => array('title', 'editor', 'custom-fields', 'thumbnail', 'excerpt') 

)); }

见我已经加入此代码有 'has_archive'=>真, '重写'=>数组( '蛞蝓'=>事件),它 将正常工作。

如果您在放置此代码后无法获取页面,您必须转到永久链接并更改网址以确保它能正常工作。