wordpress中的register_post_type没有工作模板

时间:2017-06-06 04:35:32

标签: php wordpress templates

我的功能问题" register_post_type"在wordpress。

我在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')
  ));
}

我添加了模板:single-events.php和archive-events.php,但这没有用...每次show:index.php ..

single.php没有为活动工作......

哪里有问题?谢谢!

2 个答案:

答案 0 :(得分:2)

我认为你遗失了page-events.php

创建帖子类型events后。在您的WP管理员中,创建一个名为events的页面,然后在代码中创建一个页面page-events.php,然后显示您在自定义帖子类型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 :(得分:0)

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,

'has_archive'   => true,

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

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

)); }

看到我在那里添加了这段代码 'has_archive'=&gt;真正, 'rewrite'=&gt; array('slug'=&gt; events'), 它会正常工作。

如果您在放入此代码后无法获取该页面,则必须转到固定链接并更改其确定它将起作用的URL。