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

Joren Wouters
Joren Wouters
2,306 Points

Link to Custom Post Type does not work

I made my own custom post type for my WordPress Theme, but it does not work properly. Because when I click on the link to the post, it says that it doesn't exist.

I made the custom post type with the following code:

function portfolio_custom_post_type() {
    $args = array(
      'public' => true,
      'label'  => 'Portfolio',
      'labels' => array(
                    'name' => 'Portfolio',
                    'singular_name' => 'Project',
                    'menu_name' => 'Portfolio',
                    'all_items' => 'Projects',
                    'add_new' => 'Add Project',
                    'add_new_item' => 'Add Project'
                  ),
      'description' => 'Hier kan je je projecten plaatsen, die daarna te zien zijn in je portfolio.',
      'show_in_nav_menus' => true,
      'show_in_admin_bar' => true,
      'supports' => array( 'title', 'editor', 'author', 'thumbnail'),
      'taxonomies' => array('category'),
      'has_archive' => true
    );
    register_post_type( 'portfolio', $args );
}
add_action( 'init', 'portfolio_custom_post_type' );

How do I make the link work properly?

Sincerely, Joren

5 Answers

Thomas van der Zel
Thomas van der Zel
16,796 Points

Hi Joren,

Have you considered using the Custom Post Type UI for creating a custom post type? I use it a lot and have never encountered any error...

As for you code: I can't really discover any flaws other then that your php might not be opened and closed properly.

Yojance Rabelo
Yojance Rabelo
1,156 Points

Go to Settings -> Permalinks and resave the permalinks.

This is a very common newbie mistake to always forget to resave the permalinks or in a more technical term, to flush the rewrite rules.

Joren Wouters
Joren Wouters
2,306 Points

Thanks for your help!

Yojance Rabelo, when I resave the permalinks and I go to the custom post, the whole page is blank, literally blank. Do you know how to fix that?

Yojance Rabelo
Yojance Rabelo
1,156 Points

Am I right/safe to assume that you have any of these files on your theme' folder? single-portfolio.php single.php

Joren Wouters
Joren Wouters
2,306 Points

No, I did not use any of the files in the video.

The code above is the only code I used.