Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to translate one custom post type slug in WPML

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

like image 239
terrorfall Avatar asked Jun 07 '17 10:06

terrorfall


People also ask

Should Slug be translated?

To create a seamless experience for your web visitors, it's important to translate slugs and ensure your URL addresses match the language of the rest of your website content.


1 Answers

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 );
    }
like image 195
Vel Avatar answered Oct 05 '22 23:10

Vel