最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

page template as a custom post type archive page

programmeradmin2浏览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 ?

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 ?

Share Improve this question asked Mar 18, 2020 at 14:11 blogobblogob 1377 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.)

发布评论

评论列表(0)

  1. 暂无评论