1. Home
  2. Docs
  3. Filters & Hooks
  4. Conditional trailing slashes

Conditional trailing slashes

If you would like to append trailing slashes for some specific post types or taxonomies and remove them from all other post types & taxonomies please make sure that “Trailing slashes” option is set to “Remove trailing slashes” in Permalink Manager settings:

Besides that, you will need to use a custom snippet and append it to your theme’s functions.php file:

Append the trailing slashes to only “categories” and “another-taxonomy-with-trailingslashes” (custom taxonomy) terms:

function pm_term_permalinks_trailing_slashes($permalink, $term) {
  if(!empty($term->taxonomy) && (in_array($term->taxonomy, array('category', 'another-taxonomy-with-trailingslashes')))) {
		$permalink = trailingslashit($permalink);
	}

	return $permalink;
}
add_filter('permalink_manager_filter_final_term_permalink', 'pm_term_permalinks_trailing_slashes', 999, 2);

Append the trailing slashes to only “posts” and “another-cpt-with-trailingslashes” (custom post type) items:

function pm_post_permalinks_trailing_slashes($permalink, $post) {
  if(!empty($post->post_type) && (in_array($post->post_type, array('post', 'another-cpt-with-trailingslashes')))) {
		$permalink = trailingslashit($permalink);
	}

	return $permalink;
}
add_filter('permalink_manager_filter_final_post_permalink', 'pm_post_permalinks_trailing_slashes', 999, 2);

How to remove the the trailing slashes for specific post types?

Analogous to description above, you neeed to set “Trailing slashes” option to “Add trailing slashes” and replace: trailingslashit with untrailingslashit inside the code snippets.

As a result, the trailing slashes will be appended to all post types & taxonomies but removed from specific post types & taxonomies defined with custom code snippet.

Tags ,
Was this article helpful to you? Yes No

How can we help?

Wait!!! Before you go...

Still not convinced? Use following discount code during Gumroad checkout and get 10% off!

BLOG

If you have any questions or need further information, please feel free to contact me via email: