• I’m developing a plugin which creates some custom post types and some custom taxonomies.
    The content of both must be printed in a certain personalized way. I have no problems with custom post types where I hook document_title_parts, the_title and the_content.

    I have problems with custom taxonomies. The first problem was to print a list of all the terms of a certain taxonomy: I solved it using a shortcode put in a normal page.

    Now my big problem is to print the posts which have a certain term associated. I don’t want the classic title/excerpt schema: the schema is totally different.

    I have only found examples where they say to copy the archive.php file in the theme into a new file called taxonomy-{name_of_taxonomy}.php and work on it. Impossible, my plugin is meant to be used by people who can choose the theme they want and will not have access to theme files.
    I also can’t create an archive template in the plugin directory because it would lose the theme structure.

    How can I solve it? Arent’t there hooks for taxonomies similar to document_title_parts, the_title and the_content that are available for post types?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    Yes, there are hooks. It depends on what functions are called. Since you don’t know what various themes will do, relying on hooks might not be very reliable. Why not create a shortcode that can be placed in page or post content that outputs what you want? Then it doesn’t matter what themes do or what templates they use or what hooks fire.

    BTW, it is possible to feed templates to a theme from a plugin, but it only works for primary templates. Templates loaded by get_template_part() really do need to be in the theme folder structure. I think you’re better off with shortcodes.

    Thread Starter mavenickster


    Thanks for your reply.

    Using a shortcode is what I have done to print the full list of terms of a certain taxonomy. I can also make the shortcode handle query vars to print the list of posts associated with a certain term, but I get a 404 error with a pretty url like this


    where “my-taxonomy” is a page containing my custom shortcode and “my-term” is the term of my custom taxonomy which I want to list the posts associated with.

    WordPress searches for a page with path “my-taxonomy/my-term/” which doesn’t exists.

    One solution which works is to make a url like this


    but I don’t like it.

    To have the url the way I want (mysite.com/my-taxonomy/my-term/) I should make a rewrite rule like this

    add_rewrite_rule('my-taxonomy/([^/]*)/?$', 'index.php?pagename=my-taxonomy&my-taxonomy=$matches[1]', 'top');

    but this way I force the page handling my custom taxonomy to have slug “my-taxonomy”: what if another page has already this slug?
    I don’t like this solution either.

    So I’m a little stuck.

    • This reply was modified 6 years, 4 months ago by mavenickster.
    Moderator bcworkz


    An URL like mysite.com/my-taxonomy/my-term/ should not 404, it should return an archive list of posts with that term assigned. Now, if there are no such posts, you should get a nothing found sort of message (varies by theme). Many people incorrectly call this a “404 error”. While similar in concept, they are very different sorts of errors.

    If that URL results in nothing found or 404, but yet there are posts with that term assigned, then something is wrong with the installation or the taxonomy was not registered properly to expect that URL to work. Such as arguments related to “public” being false.

    Even if the URL works and it lists matching posts, it will be in a way that is compatible with the theme. If your need is different, as a plugin, you could add a rewrite rule as you say. There is a different approach that would not risk a slug name collision, but it’s more convoluted than necessary. Instead, use the “template_include” filter to specify a template file in your plugin folder when the queried object is of my-taxonomy.

    Thread Starter mavenickster


    I’ll rethink about it a little. Thank you for now.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom archive for custom taxonomy’ is closed to new replies.