Programming Wordpress: 404 admin permalinks taxonomies taxonomy Wordpress
by bseanvt
20 comments
How To Flush Your Permalink Structure in WordPress When Using Taxonomies …or WordPress Taxonomies Not Working Instead I See A 404 Page
I didn’t know this, but because of a linking problem using custom taxonomies in WordPress, I was forced to find out. If you create a new taxonomy, it will not work immediately on the front facing end. Your users will be greeted by a 404, page not found instead. This isn’t ideal for obvious reasons.
You create and/or register custom taxonomies like so. You may have multiple taxonomies.
add_action('init', 'create_my_taxonomy', 0);
function create_my_taxonomy(){
register_taxonomy( 'musicians',
'post',
array(
'hierarchical' => false,
'label' => 'People who play lovely music that I like...',
'query_var' => 'musicians',
'rewrite' => array('slug'=>'musicians')
)
);
}
In your theme directory you need at “taxonomy.php” file. This will catch your custom taxonomy requests. You can get even more fancy and create a file like so “taxonomy-musicians.php” which you can use to differentiate your taxonomies look/feel. Nice and slick… however, you may be frustrated after following these two steps and be greeted with a friendly 404 page not found template instead.
Fear not, you need only visit the Admin >> Settings >> Permalinks page as a logged in admin to flush your rewrite rules and get linking and your correct custom taxonomy template file. Straight forward, unless if you didn’t know that merely visiting the Permalinks Settings page would correct the problem. But Alas, you do!
Addendum
You must also have posts using the taxonomy! Otherwise, you will still get the page 404 not found! I’m not sure if there is a work around for this at the moment. Anyone know?
Ruby on Rails: active_record hacks permalinks to_param
by bseanvt
leave a comment
Override to_param method in model to get pseudo permalinks without any work
There are a number of permalink plugins for Rails, http://www.seoonrails.com/even-better-looking-urls-with-permalink_fu, is a good one that I’ve used before. However, this involves informing the model class (has_permalink :title), adding a route, using the route in your views and controllers and of course running some migrations. If you’re willing to get 99% of the functionality for 0% of the work, just override the to_param method in your model.
This assumes you have an attribute for your model that you’d like to use for your permalink, but that’s it.
#app/models/post.rb
class Post < ActiveRecord::Base
def to_param
"#{id}-#{title.gsub(/[^a-z0-9]+/i, '-').downcase}"
end
end
That’s it. You don’t have to change your controllers, routes or views. Your URL will look something like
/posts/45-pseudo-permalinks-with-rails
Active record will strip all alpha characters from the params[:id] variable leaving you with just the integer value of your model.
So long as you don’t mind that integer in the url, it’s an easy solution that you can bake in at any point w/out having to touch the rest of your application code.


