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

Conditional trailing slashes

Permalink Manager allows to adjust trailing slashes settings for all WordPress permalinks, but in some particular cases you may need to make an exception and remove/add them for specific post types or taxonomies.

Below you can find an example how to remove the trailing slashes from all permalinks with the exception of selected taxonomies and post types. The following code snippet should be append it to your (child) 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 need to 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.

Was this article helpful to you? Yes No
Ending in
00 Days
00 Hrs
00 Mins
00 Secs
Expired!
Use coupon LIFE19 during checkout to get a lifetime license with 30% discount!
Buy Permalink Manager Pro