1

I have a custom post type registered as follows:

register_post_type( 'news',
    array(
        'labels' => $labels,
        'public' => true,
        'show_ui' => true,
        'menu_position' => 5,
        'capability_type' => 'post',
        'has_archive' => false,
        'rewrite' => array('slug' => 'news'),
        'hierarchical' => false,
        'show_in_menu' => true,
        'supports' => array( 'title', 'editor', 'thumbnail' )
    )
);

My intent is to have the permalink /news/name-of-the-news-item for the post and a /news/ page listing all the news.

I created a page with slug "news" and a template page for that but when I use the pagination to reach /news/page/2/ I get a 404, because it seems I cannot have a page and a custom post type with the same slug.

Is there a workaround or a strategy to achieve the permalink structure I need? Note that I cannot use archive-news.php for the listing, bacause I also need a text in the page editable in WP admin. Thanks.

1

I'd add 'has_archive' => true to your arguments array. That turns on archives automatically (with pagination too) for your post type. The template for the post type archive is archive-news.php.

Edit

For the editable text (sorry, missed the last paragraph), I would either use an option field or a custom sidebar (w/ text widget, I suppose). The amount of effort to get the page + cpt setup to play nicely AND be forwards compatible is vastly greater than the effort required to simply add a custom text field to an already standard piece of functionality. As a general solution, if the core API gets me at least 90% of the way there, I always try to figure out how to make it work instead of reinventing the wheel.

| improve this answer | |
  • That would be a solution, but I also need a text in the listing page that can be edited in WP admin. How can I achieve that with an archive page template? – spaceOdysseus Apr 17 '14 at 13:44
  • I edited the answer around the same time you commented. Does my edit address your question? – John P Bloch Apr 17 '14 at 13:56
  • I've set 'has_archive' => true and created archive-news.php page. I flushed the permalinks, /news/ page works but unfortunatelly when I point to /news/page/2/ I get a 404. How can I solve the issue? – spaceOdysseus Apr 17 '14 at 14:23
  • I fixed it setting the same number for wp-admin > settings > reading > blog posts to show and posts_per_page. – spaceOdysseus Apr 17 '14 at 14:37

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.