• Resolved lisamaria12

    (@lisamaria12)


    I have compiled my own WordPress block theme and work with custom post types for my projects, and everything works wonderfully, but I want to build a separate archive page for each category. I have already created a template for this under “Design -> Website Editor -> Templates -> Add Template -> Category Archive -> ‘the correct category'”. However, the template for “all archives” is always output when I go to the category page of the desired category.

    What could be the reason for this?

    function custom_post_type_projects() {
        $labels = array(
            'name'               => 'Projekte',
            'singular_name'      => 'Projekt',
            'menu_name'          => 'Projekte',
            'add_new'            => 'Projekt hinzufügen',
            'add_new_item'       => 'Projekt hinzufügen',
            'edit_item'          => 'Projekt bearbeiten',
            'new_item'           => 'Neues Projekt',
            'view_item'          => 'Projekt anzeigen',
            'search_items'       => 'Projekte durchsuchen',
            'not_found'          => 'Keine Projekte gefunden',
            'not_found_in_trash' => 'Keine Projekte im Papierkorb gefunden',
        );
    
        $args = array(
            'labels'             => $labels,
            'public'             => true,
            'show_in_rest'       => true,
            'has_archive'        => true,
            'publicly_queryable' => true,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'projekte' ),
            'capability_type'    => 'post',
            'menu_icon'          => 'dashicons-hammer', // Icon für das Menü
            'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
        );
    
        register_post_type( 'projects', $args );
    }
    add_action( 'init', 'custom_post_type_projects' );
    
    /* ********************************************************************************** */
    /*                                     Register Taxonomy                              */
    /* ********************************************************************************** */
    function custom_project_type_taxonomy() {
        $labels = array(
            'name'              => 'Projekttypen',
            'singular_name'     => 'Projekttyp',
            'search_items'      => 'Projekttypen durchsuchen',
            'all_items'         => 'Alle Projekttypen',
            'edit_item'         => 'Projekttyp bearbeiten',
            'update_item'       => 'Projekttyp aktualisieren',
            'add_new_item'      => 'Neuen Projekttyp hinzufügen',
            'new_item_name'     => 'Neuer Projekttyp',
            'menu_name'         => 'Projekttypen',
        );
    
        $args = array(
            'hierarchical'      => true, // Wenn true, dann wie Kategorien, ansonsten wie Schlagworte
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'show_in_rest'      => true,
            'query_var'         => true,
            'rewrite'           => array( 'slug' => 'projekttyp' ),
        );
    
        register_taxonomy( 'projekttyp', array( 'projects' ), $args );
    }
    add_action( 'init', 'custom_project_type_taxonomy' );
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Deviant Media LLC

    (@abretado1985)

    Your issue with the WordPress block theme not correctly displaying the custom category archive page instead of defaulting to the general archive template could stem from a few reasons. Here are some potential causes and solutions:1. Theme Hierarchy and Naming Convention

    WordPress uses a specific template hierarchy to determine which template file to use for different types of content. For category archives, WordPress looks for template files in the following order:

    • category-slug.php
    • category-id.php
    • category.php
    • archive.php
    • index.php

    Ensure your template naming follows WordPress conventions. Since you are working with a block theme and using the Site Editor to create templates, the issue might be less about naming and more about how WordPress is recognizing your template for specific categories.2. Flush Rewrite Rules

    Sometimes, WordPress doesn’t recognize new templates or changes to the permalink structure until you flush the rewrite rules. This can be done simply by going to Settings > Permalinks and clicking Save Changes without making any changes. This process rewrites the .htaccess file and refreshes WordPress’s understanding of your site structure.3. Check for Conflicts

    Plugin or theme conflicts can cause issues with template loading. Try deactivating all plugins except for those required for your custom post types and taxonomy, and see if the correct template is loaded. If it works, reactivate your plugins one by one to identify the culprit.4. Custom Query in Template

    If your category archive template is being selected but not displaying the correct posts, ensure you’re not overwriting the main query with a custom query without properly resetting it afterward. Use the pre_get_posts action hook to modify the main query if needed, rather than creating new queries in the template.5. Inspect the Template Hierarchy

    Debugging tools like Query Monitor can help you understand which template files WordPress is trying to load and why it might be falling back to the archive.php or another template. This can give you insight into where the process is breaking down.6. Custom Code for Template Selection

    If none of the above solutions work, you might need to use a more manual approach by hooking into the template_include filter to force WordPress to use your custom category archive template under specific conditions. Here’s a basic example:

    add_filter( 'template_include', function( $template ) {
        if ( is_category( 'your-category-slug' ) && !is_singular() ) {
            $new_template = locate_template( array( 'your-custom-template.php' ) );
            if ( '' != $new_template ) {
                return $new_template ;
            }
        }
    
        return $template;
    });
    

    Replace 'your-category-slug' with the slug of your category and 'your-custom-template.php' with the path to your custom template file within your theme.Summary

    Ensure your custom template is correctly named and recognized by WordPress, flush rewrite rules, check for conflicts, and consider using debugging tools to inspect the template hierarchy. If all else fails, manually force template selection with custom code.

    Thread Starter lisamaria12

    (@lisamaria12)

    Thank you very much!!! With the query monitor, I found out exactly how I need to name the templates so that they are output in the correct order. It works now.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Post Type Taxonomy Archive’ is closed to new replies.