Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wordpress rewrite hell

Featured Replies

So, I've been banging my head against this problem all day so I thought I'd throw it out there and see if anyone else can point me at a good resource.

 

I've created a custom post type "portfolio" that takes two custom taxonomies, "portfolio-category" (hierarchical, like normal categories) and "portfolio-skills" (non-hierarchical, like tags). All this works fine.

 

I use a custom permalink structure of /%category%/%postname% for my blog, and I'd like to achieve the same for my portfolio, but at the moment it's defaulting to /portfolio/%postname%/ - I've tried hooking into the rewrites every which way but I can't seem to get it to include the portfolio-category slug in the portfolio item permalink.

 

Any ideas, folks? Anyone written a definitive guide to WordPress rewrites?

 

 

PS: I don't need anyone to say "use this plugin" - I'm moving AWAY from a plugin for my portfolio and coding it all into my theme because I run too many plugins as it is.

  • Author

Can you post your register_taxonomy() function?

 

register_taxonomy(
		'portfolio-category',
		'portfolio', array(
		'label' => 'Portfolio Categories',
		'singular_label' => 'Portfolio Category',
		'hierarchical' => true,
		'query_var' => 'portfolio-category',
		'rewrite' => true,
		'public' => true,
		'show_ui' => true
		)
	);

 

I'm probably missing something really simple, but it's been a long day.

Edited by Renaissance-Design

  • Author

Tried that earlier; it affects the taxonomy slug but not the post type.

 

Ok, got a bit further.

 

Setting the slugs this way:

 

Post type:

 

'rewrite' => array('slug' => 'portfolio/%portfolio-category%')

 

And adding the following function:

 

function filter_post_type_link($link, $post)
{
   if ($post->post_type != 'portfolio') {
       return $link;
}
   if ($cats = get_the_terms($post->ID, 'portfolio-category')) {
       $link = str_replace('%portfolio-category%', array_pop($cats)->slug, $link);
    return $link;
}

}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

 

That adds the category to the post slug, but the category and portfolio index are still Donald Ducked.

Edited by Renaissance-Design

  • Author

Right, for anyone else who has this problem, here's how I solved it. This method requires a page with the slug "portfolio" to act as a placeholder, similar to the setting to use a static front page and a blog page.

 

add_action('init', 'portfolio_register');
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

function portfolio_register() {

$labels = array(
	'name' => _x('Portfolio', 'post type general name'),
	'singular_name' => _x('Portfolio Piece', 'post type singular name'),
	'add_new' => _x('Add New', 'portfolio piece'),
	'add_new_item' => __('Add New Portfolio Piece'),
	'edit_item' => __('Edit Portfolio Piece'),
	'new_item' => __('New Portfolio Piece'),
	'view_item' => __('View Portfolio Piece'),
	'search_items' => __('Search Portfolio'),
	'not_found' =>  __('Nothing found'),
	'not_found_in_trash' => __('Nothing found in Trash'),
	'parent_item_colon' => ''
);

$args = array(
	'labels' => $labels,
	'public' => true,
	'publicly_queryable' => true,
	'show_ui' => true,
	'query_var' => true,
	'menu_icon' => get_stylesheet_directory_uri() . '/images/portfolio.png',
	'rewrite' => array('slug' => 'portfolio/%portfolio-category%'),
	'capability_type' => 'post',
	'hierarchical' => false,
	'menu_position' => null,
	'show_in_nav_menus' => false,
	'supports' => array('thumbnail','title','editor')
  );

register_post_type( 'portfolio' , $args );
}

register_taxonomy(
		'portfolio-category',
		'portfolio', array(
		'label' => 'Portfolio Categories',
		'singular_label' => 'Portfolio Category',
		'hierarchical' => true,
		'query_var' => 'portfolio-category',
		'rewrite' => array('slug' => 'portfolio'),
		'public' => true,
		'show_ui' => true
		)
	);

function filter_post_type_link($link, $post)
{
   if ($post->post_type != 'portfolio') {
       return $link;
}
   if ($cats = get_the_terms($post->ID, 'portfolio-category')) {
       $link = str_replace('%portfolio-category%', array_pop($cats)->slug, $link);
    return $link;
}
}

 

That was a lot harder than it should have been.

Edited by Renaissance-Design

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.