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

How is that again? Looks like you started talking about custom post types and mixed in pages as well...

What if you weren't using a plugin? I know you have to register the post type first but:

Do you still need the comment section on the php file to get a template to be recognized?

Is the slug the name given to the custom post type when it is being registered? e.g.

register_post_type( 'slugforposttype', array( 'lables' => array( 'name' => ( 'nottheslug'), 'singular_name' => ('alsonottheslug'), ), 'public' => true, 'has_archive' => true, ) ); }

Why did you have to name a post Portfolio? Does that mean the post name is the slug for a page-theslug.php file?

3 Answers

You're confusing two uses of the word "slug". When you're registering a CPT in WordPress, you have to specify a unique identifier for the custom post type. In the documentation, this is sometimes called the 'slug', and as you pointed out, it can be used to target a particular theme template file to apply to all posts of that type (not pages, as you wrote).

However, when you're creating a post (even if it's a CPT) each post needs a unique 'slug' that is used in its URL to tell WordPress which post (or page, for that matter) you want to display. This is often automatically generated when you enter the title of the post and hit update - WordPress strips out special characters, replaces spaces with '-' and makes everything lower case. And yes (just to add to the confusion), the slug of a post (or a page) can also be used to help target a very specific template in your theme's PHP files so that a specific template can be used only for that exact page or post, eg: page-theslug.php or single-theslug.php.

Tom,

Thank you for your reply. Actually, I'm not the one who mixed definitions with the use of the term slug. Looks like that reality is built into WordPress. Yeah I get what amounts to a mod-rewrite on an apache server with respect to the user faced URL. Because in the video they were using the exact same names, post name, CPT (albeit the CPT was all lower case) and using a plugin instead of adding the CPT via the functions.php file and instead of putting the name of the php file being referenced they put very similar text as "identifiers" to demonstrate that the page being used was the intended page, it was difficult to deduce cause and effect for some of the examples.

But from your reply I'm certain that you have this sorted out. So...

  1. The "slug" with respect to a CPT is the declared identifier when adding a CPT via the functions.php file -reference "slugforposttype" in my first question?
    a. did a trail and error substitution drill on a theme I'm working with and that seems to work but I'm not certain
  2. The term "slug" in WordPress references a multitude of divergent items in WordPress and you need a context reference to determine what is specifically being referred to? Or does the term "slug" actually only reference the user facing portion of a URL for a specific item? And if it is a reference to the URL, can you open the page in the admin and use the correct part of the displayed URL as the "slug" needed for a given task?
  3. Stated another way, how do you find the proper "slug" to use for a given purpose? If that works out to be a long explanation, I'm good with a one liner like, "it depends on the context that it's used in".

My goal is trying to get a working knowledge so that I can code better for specific results.

Thanks again. :-)

Hey Sean, sorry, didn't mean to imply that you didn't have it figured out.

I don't mean to come across as a douche, so forgive the short reply. But smarter people than me have written some great documentation on this stuff, so I'm just going to past some links in here that you probably already know, but should be written down for posterity:

To completely figure out how WordPress uses slugs (and here I mean BOTH the URL of the page as well as the unique CPT identifier), understanding the Template Hierarchy is paramount: https://developer.wordpress.org/themes/basics/template-hierarchy/. I love this visual guide here: https://wphierarchy.com.

Custom Post Types have a unique identifier which is called "slug" in some of the docs as we've already discussed. Here are some awesome resources: https://codex.wordpress.org/Post_Types covers the basics. Then familiarize yourself with the register_post_type() function: https://codex.wordpress.org/Function_Reference/register_post_type. And https://generatewp.com/post-type/ is an awesome resource that will save you a crap-ton of typing when creating your own custom post types.

I know this doesn't completely answer your questions above, but I think these docs will do a better job than I ever could in terms of giving your the best practices.

Tom,

Thanks again! Will check all that out. Really appreciate the response!