slug - Custom Post Type with Same Page URL

admin2025-01-07  6

I have set up a page with the URL slug "announcements"

I also have a custom post type with the rewrite set to the same name:

'rewrite' => array('slug' => 'announcements'),

This all works fine.

It gets tricky here though. On my announcements page, I have a loop, with a Prev and Next page link after the loop.

So if I try to go to page 2 , it looks for the URL /announcements/page/2/ which is a 404.

My guess is this is conflicting with the URLs for the custom post type.

Is there any workaround to make this functional, or am I stuck with changing either the CPT slug or the page name?

Also, the CPT is set to have no archive, if that makes a difference.

Here are the args for my CPT:

$args = array(
        'label'                 => __( 'Announcements', 'textdomain' ),
        'description'           => __( 'Announcements Custom Post Type', 'textdomain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 4,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => true,
        'publicly_queryable'    => true,
        'menu_icon'             => 'dashicon-here',
        'capability_type'       => 'post',
        'rewrite' => array('slug' => 'announcements'),
    );

Thanks

I have set up a page with the URL slug "announcements"

I also have a custom post type with the rewrite set to the same name:

'rewrite' => array('slug' => 'announcements'),

This all works fine.

It gets tricky here though. On my announcements page, I have a loop, with a Prev and Next page link after the loop.

So if I try to go to page 2 , it looks for the URL /announcements/page/2/ which is a 404.

My guess is this is conflicting with the URLs for the custom post type.

Is there any workaround to make this functional, or am I stuck with changing either the CPT slug or the page name?

Also, the CPT is set to have no archive, if that makes a difference.

Here are the args for my CPT:

$args = array(
        'label'                 => __( 'Announcements', 'textdomain' ),
        'description'           => __( 'Announcements Custom Post Type', 'textdomain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields'),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 4,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => false,
        'exclude_from_search'   => true,
        'publicly_queryable'    => true,
        'menu_icon'             => 'dashicon-here',
        'capability_type'       => 'post',
        'rewrite' => array('slug' => 'announcements'),
    );

Thanks

Share Improve this question edited May 25, 2018 at 15:17 user1702965 asked May 25, 2018 at 12:49 user1702965user1702965 1214 bronze badges 3
  • How is your CPT registered exactly? What are the params for register_post_type? – Krzysiek Dróżdż Commented May 25, 2018 at 13:55
  • @KrzysiekDróżdż Added my args to the question. Thanks – user1702965 Commented May 25, 2018 at 15:17
  • how could there be a page 2 of the archives if you're not allowing archives? – rudtek Commented May 25, 2018 at 15:50
Add a comment  | 

1 Answer 1

Reset to default 0

How could there be a page 2 of the archives if you're not allowing archives?

This is also why it's working that you can have an actual page at that url and add a loop...you're adding that loop.

If you change your url "announcements" to something else you'll still see the first page of your cpt because presumably you've added your own custom loop.

You should not be rewriting your cpt permalinks to a page you have or create a page with the same url as your cpt permalink. It leads to confusion as you see in this case.

If you turn on the archive of this cpt, refresh your permalinks, you'll see that your content is no longer available.

Id suggest changing the slug of the page url to something else. That's the way Wordpress is intended to be used.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260614a678.html

最新回复(0)