• Resolved ModestUs

    (@modestus)


    Hi there,

    I’m trying to create a custom template for my new custom post type.

    I’ve tried creating a single-[my-post-type].php, but for some reason that template does not get used. After some research I’ve discovered that my custom post type for some reason uses the default page.php – not single.php. Just for the fun, I tried creating a page-[my-post-type].php, but with no luck.

    When I inspect the page in the browser I can see that the <body> tag gets a class of “type-attachment”.

    I am mystified. Can anybody help?

    Thanks ??

    //Rasmus

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Some fun information for you.

    Attachments are technically posts in a post type. This includes slugs. My bets are that you have an attachment that has the same name as the post type in question, and the attachment is getting interpreted first, as being the content being queried for.

    Due to the attachment detail, the attachment-based template files would be picked up, but not something like page-[my-post-type].php and similar. I assume the post type is more important than an attachment slug, so if I was in the situation, I’d end up locating the offending attachment and rename the post_name value in the db.

    Thread Starter ModestUs

    (@modestus)

    Thanks, that was the answer. Turns out I had an attachment with the same title as the specific post. After changing that, everything works as it should.

    But… How can you avoid this problem? If someone were to upload a new attachment and name it the same as an existing post, won’t the problem be back?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Very likely that would return, at least for that post. I can’t say there’s any great way to prevent this type of thing, but from my experiences, it’s also been a very infrequent issue as well. I know CPTUI compares potentially saved slugs against existing pages and other post types, but we don’t have it set up to compare against the post_name value from the database. Given the amount of posts that can easily accumulate in the database, that’d get server intensive pretty quickly.

    My take is that it’s an edge case situation that’s best left to when it happens to come up.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Template for custom post type’ is closed to new replies.