• Resolved luKkw

    (@lukkw)


    Hello!
    I would like to make my Parent Custom Post Type to list all its children CPT, and use for each for each of them (parent and children) a different template.
    So, I understand that the best way to go is to use for the parent CPT an archive-CTP.php, and for the children a single-CPT.php.
    I created both of them, and you can see here the code I used within function.php to create the CPT :

    <?php function my_custom_post_loi() {
      $labels = array(
        'name'               => _x( 'Lois', 'post type general name' ),
        'singular_name'      => _x( 'Loi', 'post type singular name' ),
        'add_new'            => _x( 'Ajouter', 'loi' ),
        'add_new_item'       => __( 'Ajouter une nouvelle loi' ),
        'edit_item'          => __( 'Editer une loi' ),
        'new_item'           => __( 'Nouvelle loi' ),
        'all_items'          => __( 'Toutes les lois' ),
        'view_item'          => __( 'Voir la loi' ),
        'search_items'       => __( 'Rechercher une loi' ),
        'not_found'          => __( 'Aucune loi trouvée' ),
        'not_found_in_trash' => __( 'Aucune loi trouvée dans la corbeille' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'Lois'
      );
      $args = array(
            'labels'        => $labels,
            'description'   => 'Les textes du Droit Malgache',
            'public'        => true,
            'menu_position' => 5,
            'supports'      => array( 'title', 'editor', 'excerpt' ),
            'has_archive'   => true,
            'publicly_queryable' => true,
            'show_ui' => true,
            'query_var' => true,
            'rewrite' => array('slug' => 'lois', 'with_front' => true),
            'capability_type' => 'post',
            'hierarchical' => true,
        );
    	flush_rewrite_rules( false );
        register_post_type( 'lois', $args );
    }
    add_action( 'init', 'my_custom_post_loi' );
    ?>

    My problem is that when I display a parent CPT, it uses the single-CPT.php instead of the archive-CPT.php. Actually, it doesn’t even use the standard archive.php, only the single-CPT.php.
    The child-CPT one is working fine, it uses the single-CTP.php as well.

    I’m pretty sure i missed (or messed) something, but i don’t know what.

    Someone got an idea ?

Viewing 5 replies - 1 through 5 (of 5 total)
  • luKkw

    It seems you’re trying to create some hierarchy like CPT named Animals, then have a Parent post named Mammals, and under that child posts like Humans, Monkeys. Correct me if I’m wrong, but if thats what you’re trying to do, then both the posts, Parent as well as Child, are going to use single-animals.php as both of these are not Archives, but just single posts of that custom post type. But the archive template will only be loaded when you open page like

    https://example.com/animals/

    That will show the archive of all the posts of custom post type animals and will be using the template archive-animals.php

    Now in case you want to use different template for Mammals(parent) and Humans(child) you can do that like this:

    single-animals.php

    <?php
    global $post;
    if($post->post_parent === 0){
        include STYLESHEETPATH . '/custom-parent-template.php';//Load template for parent
    }
    else{
        include STYLESHEETPATH . '/custom-child-template.php'; //Load template for child
    }

    and have the code for parent and child in respective files as mentioned in the code.

    Thread Starter luKkw

    (@lukkw)

    This is brilliant! – yet so simple
    I’m a newbie on coding (and wordpress) and even though I can understand a little bit the language, I’ve never mastered basics. Now I know what I’m missing.

    I managed to find a solution by installing a plugin that allowed me to choose different templates on CPT and change them manually everytime, but this is so much better ! (The plugin is called “WP Post Template” for those who may need it)

    Thanks you very much !

    Thread Starter luKkw

    (@lukkw)

    I managed to improve the code a little bit for those who may need a more that 1 level of hierarchy (a folder which include multiple folders which hold children files)
    The previous one doesn’t seems to work beyond one level of parent-child relationship

    This one does the trick for me

    <?php
    global $post;
    $children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'MYCUSTOMPOSTTYPE' ) );
    if ( is_single() && count( $children ) > 0 ) : ?>
    <?php include STYLESHEETPATH . '/PARENT-TEMPLATE.php';//Load template for parent ?>
    <?php else : ?>
    <?php include STYLESHEETPATH . '/CHILD-TEMPLATE.php'; //Load template for child ?>
    <?php endif; ?>

    luKkw

    Good to know to it worked out for you ??

    Your version of the code is much better at handling the child pages though I think you can remove the is_single() check as single-cpt.php is going to be included only when its a single page of that CPT.

    and always welcome ??

    Thread Starter luKkw

    (@lukkw)

    Yes, you were right again. I remove the useless is single() && and it worked perfectly. – I definitely should learn basics -_-
    Thank you for all your help !

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom post type archive load single-CPT.php’ is closed to new replies.