Skip to content Skip to sidebar

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 11:00 – 15:00 11:00 – 15:00 11:00 – 15:00 11:00 – 15:00 11:00 – 15:00 -
- 16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 16:00 – 20:00 -

Supporter timezone: Europe/Rome (GMT+02:00)

This topic contains 1 replies, has 2 voices.

Last updated by Laura 1 year, 9 months ago.

Assisted by: Laura.

Author Posts
October 20, 2023 at 1:26 pm #14623843

maverickM

Hello
i'm using this code to add '/news/' slug to my default posts urls
function custom_post_permalink_structure($permalink, $post) {
if ($post->post_type === 'post' || $post->post_type === 'casinos') {
$language_prefix = '';
if (function_exists('icl_object_id') && function_exists('wpml_get_current_language')) {
$current_language = wpml_get_current_language();
$default_language = wpml_get_default_language();
if ($current_language !== $default_language) {
$language_prefix = $current_language . '/';
}
}
if ($post->post_type === 'post'){
$permalink = home_url($language_prefix . 'news/') . $post->post_name;
}
if ($post->post_type === 'casinos'){
$permalink = home_url($language_prefix) . $post->post_name;
}
}
return $permalink;
}
add_filter('post_link', 'custom_post_permalink_structure', 10, 2);
add_filter('post_type_link', 'custom_post_permalink_structure', 10, 2);

function custom_pre_get_posts($query) {
if (!is_admin() && $query->is_main_query()) {
if (strpos($_SERVER['REQUEST_URI'], '/news/') !== false) {
$query->set('post_type', 'post');
} elseif (!is_page() && !is_archive() && !is_home() && !$query->get('pagename')) {
$query->set('post_type', 'casinos');
}
}
}
add_action('pre_get_posts', 'custom_pre_get_posts');

function custom_rewrite_rule() {
add_rewrite_rule('^news/([^/]+)/?$', 'index.php?post_type=post&name=$matches[1]', 'top');
add_rewrite_rule('^([a-z]{2})/([^/]+)/?$', 'index.php?post_type=casinos&name=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_rule');
but something wrong with alternate links, missing slash. Here is example hidden link
you can find that there is missing slash between '/news' and article url slug
should be hidden link

October 23, 2023 at 9:46 am #14635437

Laura
WPML Supporter since 05/2018

Languages: English (English ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Hi, can you tell me from where I can access the post with the wrong URL?

If I go to hidden link and try to open any post the URLs are correctly using the slash

The topic ‘[Closed] custom_post_permalink_structure – missing "/"’ is closed to new replies.