Archive template not working for custom post type

I am registering a custom post type called "Products & Services". I have created a file within wp-content

I am registering a custom post type called "Products & Services". I have created a file within wp-content/themes/theme/ called archive-products&services.php but it's not using this file.

I also tried archive-products & services.php, archive-productsandservices.php but still it wont load the template.

register_post_type( 'Products & Services',
    array(
        'supports' => array('title', 'editor', 'thumbnail'),
        'labels' => array(
            'name' => __( 'Products and Services' ),
            'singular_name' => __( 'Service' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'productsandservices')

    )
);

It works fine on two other custom post types called 'news' and 'products'. Is this an issue with blank spaces in the post type?

I am registering a custom post type called "Products & Services". I have created a file within wp-content/themes/theme/ called archive-products&services.php but it's not using this file.

I also tried archive-products & services.php, archive-productsandservices.php but still it wont load the template.

register_post_type( 'Products & Services',
    array(
        'supports' => array('title', 'editor', 'thumbnail'),
        'labels' => array(
            'name' => __( 'Products and Services' ),
            'singular_name' => __( 'Service' )
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'productsandservices')

    )
);

It works fine on two other custom post types called 'news' and 'products'. Is this an issue with blank spaces in the post type?

Share Improve this question asked Sep 4, 2019 at 15:26 HippoDuckHippoDuck 1374 bronze badges 2
  • The post type name should be all lowercase with underscores. Use the labels for the human-readable name for the post type. This will be how it’s done in any example that you can find. – Jacob Peattie Commented Sep 4, 2019 at 15:56
  • Thanks. The examples I found were single worded. – HippoDuck Commented Sep 4, 2019 at 16:35
Add a comment  | 

3 Answers 3

Reset to default 1

Your problem was the & which WordPress cannot support as is. That is because & is used as part of a URL to join query vars together. Like this:

example/file.php?foo=bar&baz=zoo

Also, as pointed out in comments file names have to follow the naming standard. Which is clearly laid out in the documentation.

The format is archive-[slug].php

That's why when you renamed the file it started working.

Wordpress doesn't support special characters in post types.

However, it would be useful to point out the codex doesn't state this. All it says is (max 20 characters, cannot container capital letters, underscores or spaces)

https://codex.wordpress/Function_Reference/register_post_type

After changing the post type to "Products and Services" it works with the file name "archive-productsandservices.php". I guess Wordpress doesn't support special characters in post types.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745184045a4615551.html

相关推荐

  • Archive template not working for custom post type

    I am registering a custom post type called "Products & Services". I have created a file within wp-content

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信