1

I have been looking around for the past few hours and I am starting to be kind of desperate...

I have created a plugin to add my custom post type "project" (making a new portfolio)...

In this plugin, I have the function file, and a template file single-project.php and also a template part folder with content-single-project.php within it. On that part, everything is working fine just as I want...

To be able to load the template file from the plugin folder, I did the following in the function file:

function get_custom_post_type_single_template($single_template) {
    global $post;

    if ($post->post_type == 'project') {
        $single_template = dirname( __FILE__ ) . '/templates/single-project.php';
    }
    return $single_template;
}

function get_custom_post_type_archive_template($archive_template) {
    global $post;

    if ($post->post_type == 'project') {
        $archive_template = dirname( __FILE__ ) . '/templates/archive-project.php';
    }
    return $archive_template;
}

function get_custom_post_type_category_template($category_template) {
    global $post;

    if ($post->post_type == 'project') {
        $category_template = dirname( __FILE__ ) . '/templates/category-project.php';
    }
    return $category_template;
}

add_filter( 'single_template', 'get_custom_post_type_single_template' );
add_filter( 'archive_template', 'get_custom_post_type_archive_template' );
add_filter( 'category_template', 'get_custom_post_type_category_template' );

Since it is working fine for the single template file, I don't see why I have a problem making it work for a category (or even archive) page... The category is not a custom taxonomy, it is the default (if I'm correct.. ) Category of Wordpress, same for Tags...

They are added in the project CPT while registering the new post type in the function file of the plugin, like so:

        'taxonomies' => array( 'post_tag', 'category' ),

They are "classic" right ?

Well... My primary navigation is displaying those categories (I have 3, Frontend, Design, Apps... ). When clicking on any of those links, redirecting to an URL like "root/category/post-name/", the 404 template is loaded, not the category template from the plugin folder, neither the one from the theme folder, neither archive... etc. Just the 404 template.

My permalinks are flush, I tried all the posts I have found online and nothing is changing, still not working...

I hope you guys will understand my english and will be able to help, Feel free to ask any question, Thank you for your time.

5
  • Do you have this in the same function where you register the cpt? register_taxonomy_for_object_type('category', 'project');
    – shanebp
    May 28, 2015 at 20:39
  • No I don't have that @shanebp since, if I'm correct, Category and tags are "default" ones. Should I ? This would make in some way custom taxonomies ? even tho it is the "category" slug ? And then my category-project.php template loaded from the plugin floder would work ?
    – JetXS
    May 28, 2015 at 20:46
  • Did you try it? Regardless of your assumptions?
    – shanebp
    May 28, 2015 at 21:09
  • I am going to @shanebp and I will get back here with an answer :) thanks for the idea !
    – JetXS
    May 28, 2015 at 21:10
  • Note that in the last 2 filter functions you are setting $single_template, but returning the default template.
    – shanebp
    May 28, 2015 at 21:26

2 Answers 2

1

When clicking on any of those links, redirecting to an URL like "root/category/post-name/", the 404 template is loaded,

If you remove the category_template filter, are the links still 404?

For categories, your cpt post should appear in the list of posts in that category. So the url is root/category/.

When clicking on any of those cpt links, the single template should load and the url would be root/project/post-name/

1
  • When I remove the category template filter, the links still go on the 404 template. The CPT posts doesn't appear in the root/category/ link, even with a basic querry. root/category/ load the 404 template. From the index page (index template), the posts from all categories are displaying and the root/project/post-name/ template is loading/working when clicking on one of those project.
    – JetXS
    May 28, 2015 at 21:13
1

Thanks to @shanebp, I have found the answer to the problem.

When registering a CPT, the taxonomies by default "category" and "tag" are the same as the one from the regular post. It means that the template needed to display the category related items is only the regular category.php. Therefore, my mistake was to thing of something like category-project.php (like it seems it was advertised on a few websites).

To have the categories from only a particular CPT, a custom taxonomy need to be created. Once the custom taxonomy is created taxonomy-yourtaxonomyslug.phpshould be working fine, or taxonomy-yourtaxonomyslug-thespecificterm.phpfor a specific term of the taxonomy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.