Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help fixing post permalink rewrite based on category. WordPress

  • REFUNDED

The code below I found a while back. The person who wrote it hasn't responded to my questions and every where else I looked everyone kept saying it wasn't possible. I used this code for a long time now but recently after updating WordPress it's giving me some trickery.

What it does is if I have a post called "The Food And Wine Restaurant" added to Food category and the Wine category it will cause the permalinks to update based on which section a visitor is viewing.

Example:
XXXXXXX.com/dining/food/the-food-and-wine-restaurant/
XXXXXXX.com/dining/wine/the-food-and-wine-restaurant/

I need this done for a couple reasons but the main reason is I have different child templates assigned to each section. Colors change, header changes, the layout of the page changes based on the section you are in. If I didn't do this then if someone was viewing that post in WINE it may display my FOOD theme instead.

Now it works fine but when a post is loaded ( or I am in a category with a post ) which contains multiple categories it rewrites all links on my page for widgets which supply me with such things as most visited, recently added and etc. The titles and information are correct, it just forces the LINK to rewrite to the current post I'm view. So clicking any of the links it goes back to my current page.

Is there something in the code below that needs to be updated with new 3.5 standards? I've tried several plugins that display recently added post or what's new, top post and etc but it always overwrites the links in each plugin which makes me think the code below might be outdated.

function multiple_category_post_link($url = '')
{
// check permalink structure for the required construct; /%category%/%postname%/
if (strrpos(get_option('permalink_structure'), '%category%/%postname%') !== false)
{
// get the current post
global $post, $wp_query;

// prepare variables for use below
$post_id = $cat_id = 0;
$new_url = '';

// for categories
if (is_category())
{
// remember current category and post
$cat_id = get_query_var('cat');
$post_id = $post->ID;

// add the post slug to the current url
$new_url = $_SERVER['REQUEST_URI'] . $post->post_name;
}

// for single posts
else if (is_single())
{
// last part in the 'category_name' should be the slug for the current category
$cat_slug = array_pop(explode('/', get_query_var('category_name')));
$cat = get_category_by_slug($cat_slug);

// remember current category and post
$post_id = $wp_query->post->ID;
if ($cat) $cat_id = $cat->cat_ID;

// replace the slug of the post being viewed by the slug of $post
$new_url = str_replace('/' . get_query_var('name'), '', $_SERVER['REQUEST_URI']) . $post->post_name;
}

if ($post_id > 0 && $cat_id > 0 && !empty($new_url))
{
// make sure categories match!
foreach(get_the_category($post_id) as $cat)
{
if ($cat->cat_ID == $cat_id)
{
$url = $new_url;
break;
}
}
}
}

// always return an url!
return $url;
}
add_filter('post_link', 'multiple_category_post_link');

Answers (1)

2013-02-03

Naveen Chand answers:

I guess in the last line you may have to try and replace <strong>post_link</strong> with <strong>post_permalink</strong>.


twdesigns comments:

I tried to change post_link to post_permalink but it caused the whole function to not work. I tried the_permalink which almost worked. It allowed all the plugins to work except Recent Posts, which comes with WordPress.

The current posts permalink is still getting injected into it.

Example:
While viewing XXXXXX.com/food/spanish/food-and-wine-restaurant

The recent widget showed other posts what should be, lets say Hotels, as the following:
XXXXXX.com/food/spanish/lodging/hotel/some-hotel/

It should be
XXXXXX.com/lodging/hotel/some-hotel/


Naveen Chand comments:

Good to know that the_permalink almost works. And now if it is only the wordpress recent posts widget that doesnt work, then why not install alternative plugins for recent posts? There are number of them out there.


twdesigns comments:

I would rather find a solution in case I find that it breaks something else later on. I tried a different "Recent" plugin and it still broke it as well. Any other suggestions?


Naveen Chand comments:

You should try feedburner for recent posts.

If it is still not working, then the problem could be attributed to the conflict between this script and the rss feed that wordpress is generating for your site's recent posts.

I understand it may not be an elegant solution but remember that wordpress core (especially the latest version) makes it complicated to have multiple permalinks for the same post.

If you want, here is an alternative thought/lead...

You could try to send a GET variable to the url with the category name and access the GET variable in your single.php and then conditionally change the style of that page based on that GET variable. This solution would not require the code you are using but would need to tweak your templates a bit. This is just a thought you could explore.