skip to Main Content

WordPress 6.0.1, Twenty twenty-One theme. I’m writing a plugin to display an image and some related information by the way of custom post_type and custom taxonomy.

I want to get a list of all my custom posts in front, and I’m also trying to display them singly no matter what theme is used. For this purpose and for my custom post_type = ‘hob-ap‘, I’ve created a page named archive-hob-ap.php and single-hob-ap.php with some contents, trying to respect the WordPress Template Hierarchy: https://developer.wordpress.org/files/2014/10/Screenshot-2019-01-23-00.20.04.png

Then, I flush my permalinks to update wordpress with my code. Results? I’m getting a 404 error at mysite/hob-ap instead of the archive page, and the single post content mysite/hob-ap/single-post-title display the basic template instead of the one with my changes.

Here’s a part of my code to show you my custom post-type and one taxonomy example if it can help from hob-ap.php:

function hob_post_type() {

    $labels = array(
        'name'                => _x( 'HOB Publicités', 'Post Type General Name'),
        'singular_name'       => _x( 'HOB Publicité', 'Post Type Singular Name'),
        'menu_name'           => __( 'HOB Publicité'),
        'all_items'           => __( 'Toutes les publications'),
        'view_item'           => __( 'Voir les publications'),
        'add_new_item'        => __( 'Ajouter une nouvelle publication'),
        'add_new'             => __( 'Ajouter une publication'),
        'edit_item'           => __( 'Editer la publication'),
        'update_item'         => __( 'Modifier la publication'),
        'insert_into_item'    => __('Insérer dans HOB Publication'),
        'uploaded_to_this_item' => __('Uploader la publication'),
        'search_items'        => __( 'Rechercher une publication'),
        'not_found'           => __( 'Non trouvée'),
        'not_found_in_trash'  => __( 'Non trouvée dans la corbeille'),
    );
        
    $args = array(
        'label'               => __( 'HOB Publicité'),
        'description'         => __( 'Organisez vos PDF protégés'),
        'menu_icon'           =>  plugins_url('/asset/img/logo.png', __FILE__),
        'labels'              => $labels,
        'supports'            => array( 'title', 'custom-fields'),
        'show_in_rest'        => true,
        'hierarchical'        => false,
        'public'              => true,
        'has_archive'         => true,
        // 'rewrite'              => array( 'slug' => 'hobpage'),
    );  
    register_post_type( 'hob-ap', $args );
}

add_action( 'init', 'hob_post_type', 0 );
add_action( 'init', 'hob_add_taxonomies', 0 );

function hob_add_taxonomies() {
   $args_registry = array(
        'hierarchical'          => false,
        'label'                 =>__( 'N° de registre'),
        'show_ui'               => true,
        'show_in_menu'          => false,
        'show_in_rest'          => true,
        'show_admin_column'     => true,
        'query_var'             => true,
        'meta_box_cb'           => 'hob_add_register_number',
        'rewrite'               => array( 'slug' => 'hob_register' ),
    );
    register_taxonomy( 'hob_register', 'hob-ap', $args_registry );  

}

I’ve also tried to rewrite the slug as you can see it commented, and modify for example my archive-hob-ap.php to archive-hobpage.php before flushing my permalinks, but I get the same result, also with 'has_archive' => true or false

Many thanks in advance!

2

Answers


  1. Chosen as BEST ANSWER

    Problem solved. Everything was right, but I was missing an hook in order to perform a link between my custom plugin and the theme. Here is the solution, hope it can help others members:

    function get_hob_ap_archive_template( $archive_template ) {
            if ( is_post_type_archive ( 'hob-ap' ) ) {
                 $archive_template = dirname( __FILE__ ) . '/archive-hob-ap.php';
            }
            return $archive_template;
        }
        add_filter( 'archive_template', 'get_hob_ap_archive_template' ) ;
    
    

  2. Please try to reset permalinks, as you mentioned you flushed it already, can you please confirm is it set to plain (?p=page_id) and default?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search