logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Help fixing post permalink rewrite based on category.

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');

TWDesigns | 02/02/13 at 7:45pm Edit
Tutorial: How to assign prize money


(5) Responses

Note: TWDesigns felt their question was unanswered, so we granted them a refund.

Note: TWDesigns requested a refund. They offered this explanation:

"No one answered my question / fixed my problem and only provided me with alternate examples."

If no one challenges a refund request, then they are automatically granted and proccessed after 48 hours. Admins of this site only review refund requests if someone challenges the request. If you are curious about how we handled previous refund requests, you may read over all refund requests and their challenges.

See a threaded view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    02/02/13
    8:09pm
    Naveen Chand says:

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

  • avatar
    Last edited:
    02/02/13
    9:03pm
    TWDesigns says:

    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/

  • avatar
    Last edited:
    02/03/13
    4:44am
    Naveen Chand says:

    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.

  • avatar
    Last edited:
    02/04/13
    2:43pm
    TWDesigns says:

    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?

  • avatar
    Last edited:
    02/04/13
    6:17pm
    Naveen Chand says:

    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.

This question has expired.





Current status of this question: Refunded



Please log in to add additional discourse to this page.





Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.