Categories for Pages and permalink
-
Hello,
I found a good plugin to integrate the categories for posts which are actually pages : http://amit.me/wp-plugins
Although, I am now looking for a way to change the permalinks for pages so they can be like : http://www.example.com/category-name/page-name.
I can’t find anything on the web to do this. Would you please have any idea?
Thank you,
Regards.
-
You can use the Rewrite API to define new perma-structures. Be careful how you set this up, the parser needs some way to know that the first path element is a category and not something like a post slug. One way is to force all permalinks to have a category parameter as the first element. Another is to precede any category with an identifying term, like
example.com/cat/politics/page-name/.Without doing anything differently, by default WP will ignore most preceding terms if a page slug is provided since the page slug is all that is needed to locate a post. There cannot be duplicate slugs, so a category specification is superfluous. The exception is if WP recognizes the preceding term as something, such as a year. The term is then made part of the query, if there is a mismatch (specifying 2013 for a post made in 2014, for example) then no post will be returned.
Thank you for your reply bcworkz. I understand what you mean and that is what I want. Nevermind if each page or post has an url like: http://www.example.com/cat-name/page-or-post-name. That would actually be awesome.
I will have a look through the Rewrite API. Although, I am quite new in WordPress so this sounds very unclear to me. What will I have to do? Redefine functions with filters? Configure the permalink format in the admin settings? Change my htaccess file?
Thank you.
Regards,I managed to get the URL format working thanks to the function
add_rewrite_rulesuch as:function add_page_url() { add_rewrite_rule( '^([^/]*)/([^/]*)/?', 'index.php?category=$matches[1]&pagename=$matches[2]', 'top' ); }Although, I now don’t know how to define the pages’ permalinks so they follow this new URL format.
I’m glad you figured that out! I’m not too sure I would have explained it adequately, I struggle with it myself. It’s still not entirely clear to me either 🙁
Altering permalinks is more straight forward, as long as you grasp hooking filters. What follows assumes the permalinks are coming from template tags like
the_permalink(),get_the_permalink(), or any of it’s relatives that eventually callget_permalink(). (Most themes do this) That last function has three different filters which can be useful to alter permalinks, I’m not sure which would be the best.To see how filter callback return is used in forming a permalink, it’s best to just review the source code. Hook into the one that would work best for you.
I am not sure I understand..
Everything works fine with the blog posts since I was able to configure the permalinks such as/%category%/%postname%. Although, I am not sure about to overload theget_permalinkfunction so I can have the sane structure as for the posts. Plus, I also have to take account of the language thing, which means adding thefrfor french, at the beginning of the url if needed.I don’t understand what you mean with the three different filters available for
get_permalink(). Do I have to overload this function by creating a new one and apply the filters toget_permalink()?Thank you.
There we go!
I created a filter that I hooked on the
page_linkfunction. The good thing is, this function is no only called when fetching the pages’ URLs but also when creating a new page. Therefore, when the page is added to the database, the modified permalink is too (something that I didn’t expect at all).Anyway, there is probably still a few things to fix in this code. I will make some tests now.
function add_page_url() { add_rewrite_rule( '^([^/]*)/([^/]*)/page/([^/]*)?', 'index.php?category=$matches[1]&pagename=$matches[2]&paged=$matches[3]', 'top' ); add_rewrite_rule( '^([^/]*)/([^/]*)/?', 'index.php?category=$matches[1]&pagename=$matches[2]', 'top' ); } add_action('init', 'add_page_url'); function add_cat_page_link($link, $id) { // get page $page = get_pages(array('include'=>$id)); // get category slug $category = get_the_category($page[0]->ID); if (!empty($category)) { $slug = $category[0]->slug; } else { $slug = 'uncategorized'; } // ret new URL return site_url().'/'.$slug.'/'.$page[0]->post_name; } add_filter('page_link', 'add_cat_page_link', 0, 2);It seems that if I let you be for a while you manage to figure things out on your own! 🙂 Let us know how your testing goes and if there are any remaining issues you are unable to resolve.
Don’t worry, I will not let you be to figure things out on your own on purpose. If I have anything useful say, I will do so as soon as I see your post no matter how recent it is.
The topic ‘Categories for Pages and permalink’ is closed to new replies.