9
votes

I have raised this issue on the WPML forums, but hoping someone here will be able to assist.

I am trying to translate the slug for a custom post type

The English URL is http://brigade-electronics.com/nl/products/backeye360/

The translated URL should be http://brigade-electronics.com/nl/producten/backeye360/

Instead I get a 404 error when navigating to the URL after enabling the translate slug option

Steps to duplicate the issue:

  • Click On WPML -> Translation options
  • Enable the Translate custom posts slugs (via WPML String Translation).
  • Under the Custom posts settings (on the same page) Click the translate checkbox
  • Added the translated slug for each language
  • Hit save
  • Navigate to the front end and see a 404 error on the products section only.

I have run all options in the troubleshooting page, to clear up the database.

This only seems to apply to certain pages within the product section. The weirdest part of this is the Canadian section of the site, as the term 'product' is in English therefore the URLs remain the same with or without the translated slugs in place, however, I still get the 404 error on these pages.

It is also worth noting that all other custom post types work without issue.

The custom post types have been registered in the standard way

function register_products_post_type() {

    $labels = array(
        'name' => __( 'Products', '' ),
        'singular_name' => __( 'Product', '' )
    );

    $args = array(
        'label' => __( 'Products', '' ),
        'labels' => $labels,
        'description' => '',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_rest' => false,
        'rest_base' => '',
        'has_archive' => false,
        'show_in_menu' => true,
        'exclude_from_search' => false,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => true,
        'rewrite' => array( 'slug' => 'products', 'with_front' => false ),
        'query_var' => true,
        'menu_position' => 6,
        'menu_icon' => 'dashicons-cart',
        'supports' => array( 'title', 'thumbnail', 'page-attributes' )
    );

    register_post_type( 'products', $args );

}
add_action( 'init', 'register_products_post_type' );

As per the below answer, the above code has been updated to

add_action( 'init', 'create_post_type');
function create_post_type() {
    $labels = array(
        'name'               => _x( 'Products', 'general name of the post type' ),
        'singular_name'      => _x( 'Products', 'name for one object of this post type' ),

    );
    $args = array(
        'labels' =>  $labels, // An array that defines the different labels assigned to the custom post type
        'public' =>  true, // To show the custom post type on the WordPress dashboard
        'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
        'has_archive' =>  true, //Enables the custom post type archive at
        'hierarchical' =>  true, //Enables the custom post type to have a hierarchy
        'rewrite' => array( 'slug' =>  _x('products', 'URL slug')),
    );
    register_post_type( 'products', $args );
    }

The new translation for the slug appears in the 'String Translation' section, when updating these strings, I get the same 404 error. If I leave these as English the products section works with no problem.

Thanks

2
where is the translation slug $labels = array( 'name' => __( 'Products', '' ), 'singular_name' => __( 'Product', '' ) ); - Text Domain is missingmujuonly
This has not made a difference to all other custom post types, they are all set up the same, but will give it a try and let you knowterrorfall
Hi @MujeebuRahman adding a domain has not made a differenceterrorfall
Have u tried _e() ?mujuonly
@MujeebuRahman __e() echos the string, which won't work in this instanceterrorfall

2 Answers

1
votes

Try this

    add_action( 'init', 'create_post_type');
    function create_post_type() {
        $labels = array(
        'name'               => _x( 'Products', 'general name of the post type' ),
        'singular_name'      => _x( 'Products', 'name for one object of this post type' ),

       );
       $args = array(
         'labels' =>  $labels, // An array that defines the different labels assigned to the custom post type
         'public' =>  true, // To show the custom post type on the WordPress dashboard
         'supports' => array( 'title', 'thumbnail', 'page-attributes' ),
         'has_archive' =>  true, //Enables the custom post type archive at 
         'hierarchical' =>  true, //Enables the custom post type to have a hierarchy
         'rewrite' => array( _x('slug' => 'products'), 'with_front' => false ),
    );
    register_post_type( 'products', $args );
    }
0
votes

Have you flushed the rewrite rules?

Go to Settings > Permalinks and refresh.

Note: If registering a post type inside of a plugin, call flush_rewrite_rules() in your activation and deactivation hook (see Flushing Rewrite on Activation below). If flush_rewrite_rules() is not used, then you will have to manually go to Settings > Permalinks and refresh your permalink structure before your custom post type will show the correct structure.

source: https://codex.wordpress.org/Function_Reference/register_post_type