0

I know this kind of question have already been answered, but the answers are not clear to me !!

I have some custom post types. I created them in the "wordpress " way, so i have "archive-{mycustomPostType}".php files. It's working fine.

The problem, as you know, is that there are no such pages in the back end : no archive page in the page listing. So a client cannot edit the page.

I'm using elementor, with sections or widgets that are used in many other pages. I would like to reuse them in the archive-{mycustomPostType}".php but it's not possible :

-i cannot edit archive page with elementor because there are no "archive page".

-i cannot use shortcodes to display those widgets in my archive-{mycustomPostType}".php file, because there are no shortcodes for elementor widget.

So i thought creating some page-{mycustomPostType}".php files instead, so i can hardcode my page AND also use the_content() to display elementor widgets, throught the elementor editor.

Now the problem is that as it is a page, when i visit a custom post type post, the parent is still the archive page automaticaly created by wordpress (in the breadcrumb for example) and not my page-{mycustomPostType}".php

Question : how can i simulate the normal behaviour of the combo : archive-{mycustomPostType} + custom post type post, using page templates ??

In my custom page template, i can display a loop of all related custom posts, no problem. I have my single-{mycustomPostType}.php file, and the post is displaying correctly. No problem.

But how can i connect both to have logical "parent-children" url ?

1

You can give people an actual Page to edit, and the permalinks you want, by setting up a rewrite when you create the CPT.

Note: you will need to replace all instances of 'mycpt' with your actual CPT slug.

<?php
function wpse_360965_register_cpt() {
    // First unregister this post type so we're starting from scratch
    unregister_post_type( 'mycpt' );
    $args = array(
        // Set the CPT to not have an actual Archive
        'has_archive' => false,
        // Set a Rewrite so permalinks still fall under the URL you desire
        'rewrite' => array( 'slug' => 'mycpt' ),
        // Add your other arguments here too
    );
    register_post_type( 'mycpt' , $args );
}
add_action( 'init' , 'wpse_360965_register_cpt' );
?>

What this does is ensure that WP does not create an actual Archive - this makes it possible to create a Page instead. The rewrite ensures that individual CPTs will be published in a way that looks like they are "children" of that Page, even though they're not.

After this is run once, you can remove the unregister_post_type() line.

From here, you'll create the page-mycpt.php (again replacing the slug) template. Your needs will determine whether you need a custom Loop, or just the regular content as provided by your editor.

Finally, breadcrumbs will depend on what you're using to generate the breadcrumbs. If you are using Yoast WP SEO, you'll likely need to use filters to get the breadcrumbs to look right. For example:

<?php
function wpse_360965_filter_yoast_breadcrumbs( $links ) {
    // Only affect individual "mycpt" CPTs
    if ( is_singular( 'mycpt' ) ) {
        // Add "My CPT" breadcrumb
        $addedBreadcrumbs = array(
            array( 'text' => 'My CPT', 'url' => '/mycpt/', 'allow_html' => 1)
        );
        // Add the new breadcrumb before the single CPT title
        array_splice( $links, 1, 0, $addedBreadcrumbs );
    }
    // Always return the links, even if we didn't change them
    return $links;
}
add_filter( 'wpseo_breadcrumb_links', 'wpse_360965_filter_yoast_breadcrumbs' );
?>

(Once again, replace the 'My CPT' text and '/mycpt/' URL as needed.)

3
  • Your answer was the solution. I really appreciate your help !! the breadcrumb function didn't work, it disable the breadcrumb on all pages...quite strange.Anyway,the main issue is solved thanks to your help !! – blogob Mar 18 '20 at 15:39
  • Ah my mistake - it was missing the return line. It's fixed above. – WebElaine Mar 18 '20 at 16:46
  • it's working !! incredible..you found the solution in a minute, for me it would have take hours or days !! thank you very much !! – blogob Mar 18 '20 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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