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

custom post type and hidden url : what is the cleanest way to build CPT with custom page template?

programmeradmin2浏览0评论

I recently built a website were I needed to let user edit the custom post type archive page with elementor. So I decided to use page templates instead of archive pages, that way,I can build my pages with code (custom loop) and elementor (widgets).

But after navigating in my website, I spotted strange behaviours. I'm sorry I have many questions, but I think they are all related to the same topic : the cleanest way to build CPT with page.

1-For my page template :

http://localost/mytheme/all-lessons

If I type for example, an url with the name of my CPT,

http://localhost/mytheme/lessons (the former CPT archive url)

I'm redirected to the first post of that CPT

http://localost/mytheme/all-lessons/lesson-1

I didn't made any redirection rules so I don't understand.

For all my custom post type I use the same code, with hierarchical=> false and has_archive =>false:

$args = array(
    'label'               => _x('Formation', 'text_domain'),
    'description'         => _x('Formations', 'text_domain'),
    'labels'              => $labels,
    'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-admin-home',
    'show_in_admin_bar'   => true,
    'show_in_nav_menus'   => true,
    'can_export'          => true,
    'has_archive'         => false,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => true,
    'rewrite'             =>  array( 'slug' => 'toutes-nos-formations' ),
    'capability_type'     => 'post',
    'show_in_rest'          =>'true'
)

The strange thing is that for one of those CPTs, if I type the name of the archive page, I see a 404.

  1. So Is there a way to completely disable CPT archive page system, so no one can access to those archive pages built by wordpress and I wouldn't have those strange behaviours ?

  2. I have a custom post type for which I don't need single pages. They exist in backend because I 'm just displaying them in a grid, in a page template.

How can I do to entirely hide those single pages url ? because I dont want a user to see those page that are not styled at all.

  1. As I'm using custom page, I loose the normal wordpress navigation system (current-menu-item ) So when I'm in a single CPT, the custom page in the menu doesn't highlight. Is there a simple solution for that ?

I recently built a website were I needed to let user edit the custom post type archive page with elementor. So I decided to use page templates instead of archive pages, that way,I can build my pages with code (custom loop) and elementor (widgets).

But after navigating in my website, I spotted strange behaviours. I'm sorry I have many questions, but I think they are all related to the same topic : the cleanest way to build CPT with page.

1-For my page template :

http://localost/mytheme/all-lessons

If I type for example, an url with the name of my CPT,

http://localhost/mytheme/lessons (the former CPT archive url)

I'm redirected to the first post of that CPT

http://localost/mytheme/all-lessons/lesson-1

I didn't made any redirection rules so I don't understand.

For all my custom post type I use the same code, with hierarchical=> false and has_archive =>false:

$args = array(
    'label'               => _x('Formation', 'text_domain'),
    'description'         => _x('Formations', 'text_domain'),
    'labels'              => $labels,
    'supports'            => array('title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'menu_position'       => 5,
    'menu_icon'           => 'dashicons-admin-home',
    'show_in_admin_bar'   => true,
    'show_in_nav_menus'   => true,
    'can_export'          => true,
    'has_archive'         => false,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => true,
    'rewrite'             =>  array( 'slug' => 'toutes-nos-formations' ),
    'capability_type'     => 'post',
    'show_in_rest'          =>'true'
)

The strange thing is that for one of those CPTs, if I type the name of the archive page, I see a 404.

  1. So Is there a way to completely disable CPT archive page system, so no one can access to those archive pages built by wordpress and I wouldn't have those strange behaviours ?

  2. I have a custom post type for which I don't need single pages. They exist in backend because I 'm just displaying them in a grid, in a page template.

How can I do to entirely hide those single pages url ? because I dont want a user to see those page that are not styled at all.

  1. As I'm using custom page, I loose the normal wordpress navigation system (current-menu-item ) So when I'm in a single CPT, the custom page in the menu doesn't highlight. Is there a simple solution for that ?
Share Improve this question edited Mar 26, 2020 at 19:41 WordPress Speed 2,2833 gold badges19 silver badges34 bronze badges asked Mar 26, 2020 at 16:52 blogobblogob 1377 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Ok maybe it's not a definitive answer, but for question 3 : this thread give a solution to highlight custom posts parent menu item, even if using a page template instead of archive page.

发布评论

评论列表(0)

  1. 暂无评论