2 added 12 characters in body
source | link
/* Add custom post type 'Products' to Tower/NPI Theme */
  add_action('init', 'create_postTypeProducts', 0 );
  function create_postTypeProducts() {

$labels = array(
  'name'                => _x( 'Products', 'Post Type General Name', 'Tower-NPI'),
  'singular name'       => _x( 'Product', 'Singular Name', 'Tower-NPI'),
  'menu_name'           => _x( 'Products', 'admin menu', 'Tower-NPI'),
  'name_admin_bar'      => _x( 'Products', 'add new on admin bar', 'Tower-NPI' ),
  'all_items'           => __( 'All Products', 'Tower-NPI' ),
  'view_item'           => __( 'View Products', 'Tower-NPI' ),
  'add_new_item'        => _x( 'Add New Product', 'Tower-NPI' ),
  'add_new'             => _x( 'Add Product', 'Tower-NPI' ),
  'edit_item'           => __( 'Edit Product', 'Tower-NPI' ),
  'update_item'         => __( 'Update Product', 'Tower-NPI' ),
  'search_items'        => __( 'Search for Products', 'Tower-NPI' ),
  'not_found'           => __( 'Not Found', 'Tower-NPI' ),
  'not_found_in_trash'  => __( 'Not found in Trash', 'Tower-NPI' ),
);

$args = array(
  'label'               => __('Products', 'Tower-NPI'),
  'description'         => __('A list of products associated with the Tower/NPI Theme.', 'Tower-NPI'),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'author', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes' ),
  'hierarchical'        => true,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 21,
  'menu_icon'           => 'dashicons-admin-home',
  'can_export'          => true,
  'has_archive'         => true,
  'exclude_from_search' => false,
  'publicly_queryable'  => true,
  'capability_type'     => 'post',
  'show_in_rest'        => true,
  'rest_base'           => 'products-api',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
  'rewrite'             => array('slug' => '/', 'with_front' => false,)
);
register_post_type('Products', $args );
  }

 /* Flush rewrite rules on theme switch */
  function my_rewrite_flush() {
    my_cpt_initcreate_postTypeProducts();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
/* Add custom post type 'Products' to Tower/NPI Theme */
  add_action('init', 'create_postTypeProducts', 0 );
  function create_postTypeProducts() {

$labels = array(
  'name'                => _x( 'Products', 'Post Type General Name', 'Tower-NPI'),
  'singular name'       => _x( 'Product', 'Singular Name', 'Tower-NPI'),
  'menu_name'           => _x( 'Products', 'admin menu', 'Tower-NPI'),
  'name_admin_bar'      => _x( 'Products', 'add new on admin bar', 'Tower-NPI' ),
  'all_items'           => __( 'All Products', 'Tower-NPI' ),
  'view_item'           => __( 'View Products', 'Tower-NPI' ),
  'add_new_item'        => _x( 'Add New Product', 'Tower-NPI' ),
  'add_new'             => _x( 'Add Product', 'Tower-NPI' ),
  'edit_item'           => __( 'Edit Product', 'Tower-NPI' ),
  'update_item'         => __( 'Update Product', 'Tower-NPI' ),
  'search_items'        => __( 'Search for Products', 'Tower-NPI' ),
  'not_found'           => __( 'Not Found', 'Tower-NPI' ),
  'not_found_in_trash'  => __( 'Not found in Trash', 'Tower-NPI' ),
);

$args = array(
  'label'               => __('Products', 'Tower-NPI'),
  'description'         => __('A list of products associated with the Tower/NPI Theme.', 'Tower-NPI'),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'author', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes' ),
  'hierarchical'        => true,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 21,
  'menu_icon'           => 'dashicons-admin-home',
  'can_export'          => true,
  'has_archive'         => true,
  'exclude_from_search' => false,
  'publicly_queryable'  => true,
  'capability_type'     => 'post',
  'show_in_rest'        => true,
  'rest_base'           => 'products-api',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
  'rewrite'             => array('slug' => '/', 'with_front' => false,)
);
register_post_type('Products', $args );
  }

 /* Flush rewrite rules on theme switch */
  function my_rewrite_flush() {
    my_cpt_init();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
/* Add custom post type 'Products' to Tower/NPI Theme */
  add_action('init', 'create_postTypeProducts', 0 );
  function create_postTypeProducts() {

$labels = array(
  'name'                => _x( 'Products', 'Post Type General Name', 'Tower-NPI'),
  'singular name'       => _x( 'Product', 'Singular Name', 'Tower-NPI'),
  'menu_name'           => _x( 'Products', 'admin menu', 'Tower-NPI'),
  'name_admin_bar'      => _x( 'Products', 'add new on admin bar', 'Tower-NPI' ),
  'all_items'           => __( 'All Products', 'Tower-NPI' ),
  'view_item'           => __( 'View Products', 'Tower-NPI' ),
  'add_new_item'        => _x( 'Add New Product', 'Tower-NPI' ),
  'add_new'             => _x( 'Add Product', 'Tower-NPI' ),
  'edit_item'           => __( 'Edit Product', 'Tower-NPI' ),
  'update_item'         => __( 'Update Product', 'Tower-NPI' ),
  'search_items'        => __( 'Search for Products', 'Tower-NPI' ),
  'not_found'           => __( 'Not Found', 'Tower-NPI' ),
  'not_found_in_trash'  => __( 'Not found in Trash', 'Tower-NPI' ),
);

$args = array(
  'label'               => __('Products', 'Tower-NPI'),
  'description'         => __('A list of products associated with the Tower/NPI Theme.', 'Tower-NPI'),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'author', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes' ),
  'hierarchical'        => true,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 21,
  'menu_icon'           => 'dashicons-admin-home',
  'can_export'          => true,
  'has_archive'         => true,
  'exclude_from_search' => false,
  'publicly_queryable'  => true,
  'capability_type'     => 'post',
  'show_in_rest'        => true,
  'rest_base'           => 'products-api',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
  'rewrite'             => array('slug' => '/', 'with_front' => false,)
);
register_post_type('Products', $args );
  }

 /* Flush rewrite rules on theme switch */
  function my_rewrite_flush() {
    create_postTypeProducts();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
1
source | link

Some permalinks on Apache/localhost development setup return 404's when set to anything other than plain permalinks

I'm building a custom theme using Xampp on my localhost. I've got everything sorted for the site, except for this permalinks issue.

Currently, the site uses the Advanced Custom Fields plugin and has some dependencies on this plugin to work.

When using plain permalinks, the site works fine. When I change to "Post name" permalinks, all pages on the site return 404 errors, except for the Custom Post Types that I've setup, which function as normal pages.

I can revert back to one of the default themes, and the permalinks start working as normal, so I'm fairly confident that it's a theme issue.

When I replace the functions.php file with a blank file (no php code inside), the site loads minus the styles as expected, but all the links simply refresh the page instead of attempting to navigate to any of the pages.

Just to confirm a few things...

  • mod_rewrite is turned on in Apache, although I've also set this up on a separate Microsoft server and IIS, so I don't think this is the issue.
  • I've disabled all plugins (including the advanced custom posts), and the 404 issue still occurs
  • The pages don't return 404's when switched to the default theme
  • The home page renders normally regardless of what settings I use.
  • I'm getting the 404.php page (that I setup) returned, and not the WordPress 404 page
  • I've tried adding rules to flush the rewrite rules
  • I've tried setting global permalink rewrite structure to /%postname% which didn't make a difference
  • WordPress can write to the .htaccess file. This definitely changes or is created locally when permalink settings are updated so this isn't an issue either.

Below is my functions.php code which initializes the Custom Post to WordPress - this is pretty much the only thing that I can think of that must contain some sort of error, as all of the above steps that I've tried don't seem to have any effect.

/* Add custom post type 'Products' to Tower/NPI Theme */
  add_action('init', 'create_postTypeProducts', 0 );
  function create_postTypeProducts() {

$labels = array(
  'name'                => _x( 'Products', 'Post Type General Name', 'Tower-NPI'),
  'singular name'       => _x( 'Product', 'Singular Name', 'Tower-NPI'),
  'menu_name'           => _x( 'Products', 'admin menu', 'Tower-NPI'),
  'name_admin_bar'      => _x( 'Products', 'add new on admin bar', 'Tower-NPI' ),
  'all_items'           => __( 'All Products', 'Tower-NPI' ),
  'view_item'           => __( 'View Products', 'Tower-NPI' ),
  'add_new_item'        => _x( 'Add New Product', 'Tower-NPI' ),
  'add_new'             => _x( 'Add Product', 'Tower-NPI' ),
  'edit_item'           => __( 'Edit Product', 'Tower-NPI' ),
  'update_item'         => __( 'Update Product', 'Tower-NPI' ),
  'search_items'        => __( 'Search for Products', 'Tower-NPI' ),
  'not_found'           => __( 'Not Found', 'Tower-NPI' ),
  'not_found_in_trash'  => __( 'Not found in Trash', 'Tower-NPI' ),
);

$args = array(
  'label'               => __('Products', 'Tower-NPI'),
  'description'         => __('A list of products associated with the Tower/NPI Theme.', 'Tower-NPI'),
  'labels'              => $labels,
  'supports'            => array( 'title', 'editor', 'excerpt', 'revisions', 'author', 'thumbnail', 'custom-fields', 'post-formats', 'page-attributes' ),
  'hierarchical'        => true,
  'public'              => true,
  'show_ui'             => true,
  'show_in_menu'        => true,
  'show_in_nav_menus'   => true,
  'show_in_admin_bar'   => true,
  'menu_position'       => 21,
  'menu_icon'           => 'dashicons-admin-home',
  'can_export'          => true,
  'has_archive'         => true,
  'exclude_from_search' => false,
  'publicly_queryable'  => true,
  'capability_type'     => 'post',
  'show_in_rest'        => true,
  'rest_base'           => 'products-api',
  'rest_controller_class' => 'WP_REST_Posts_Controller',
  'rewrite'             => array('slug' => '/', 'with_front' => false,)
);
register_post_type('Products', $args );
  }

 /* Flush rewrite rules on theme switch */
  function my_rewrite_flush() {
    my_cpt_init();
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );

Any help would be hugely appreciated.

Thanks,