Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Boris Kamp
Boris Kamp
16,660 Points

Wordpress post template question

Im working with single-'custompostype name'.php files to create templates for my custom post types. Off course this works great, as my permalinks come out as sitename/custom postype name/postname

But now I ran into and issue, I created a single-contact.php file to create the layout for my contact page. There will only be one post in this post type archive! just one contact page. now the url comes out as sitename/contact/contact. I would like it to be sitename/contact.

How can I solve this? aka, What do I need to do to create a template for just one single post and avoid the double name in the url?

Thanks!

2 Answers

It sounds like you want a custom template for the contact form, rather than a custom post type. You can create custom templates for individual pages. This would allow you to style it differently from your post and other pages, and it will allow you to have the sitename/contact url you want.

  1. Just build the template page the way you want it to look.
  2. Add in a comment /* Template Name: Contact */ at the top of the template page.
  3. Create a new page named Contact and set the page template to the your new Contact template.

Then again, if you have other requirements that necessitate the use of a post type, let us know what those are.

The challenge of getting your single custom post to have the sitename/contact url is that the contact url is used as your archive for that custom post type. If you modify that url (which most likely can be done via a rewrite rule), you would effectively remove the archive as an option for that post type.

Since you only plan to have a single post, one option you have is to return the content of the custom post type within the loop on your archive page. I.e., when you execute the loop within the contact page, return the_content() rather than an excerpt. If you added the contact form in the wsyiwyg editor, the form will appear, and you will have the sitename/contact url as you desire.

That said, I would recommend just adding the contact content within a page, rather than a post type. It will be a simpler approach and doesn't run the risk of problems in the future.

Boris Kamp
Boris Kamp
16,660 Points

Exactly what I needed! Guess I should have paid more attention at that. Thank you very much!