0

Can items have multiple permalinks?

I am currently struggling to setup routing for a custom taxonomy that I have setup.

I have a custom post type, say hotels.

A standard URL for one of these posts might be /hotels/the-marriot-hotel

But I also have a custom taxonomy, Locations.

I want to be able to access each hotel via the standard URL and a URL that resembles a breadcrumb of the taxonomy, such as /Locations/United-Kingdom/the-marriot-hotel or even /Locations/Liverpool/the-marriot-hotel.

I have tried getting term link and appending $post->post_name on the end but I just get a 404 error so its not matching the route

Is this possible?

I have setup my taxonomy with a rewrite and give it a slug, so I can browse hotels in Liverpool for example with the url /Locations/Liverpool. This part works fine but when a user clicks one of the posts on this page I'd like it to follow the URL scheme for that section

Thanks

0

Natively every post in WordPress has one specific permalink (if any and not counting non–pretty arguments–based shortlinks). This makes sense not just to WordPress, but also to other entities like people and search engines.

It is certainly possible to make additional URLs to load posts in WP too.

However it won't help you any with that. You would have to:

  • construct and implement rewrite rule for it from scratch
  • be very careful about canonical links and that search engines don't see it as needlessly duplicate content
3
  • Thank you for your answer, your point about SEO is a very good one, I hadn't thought of that. Would a rewrite point that URL back to the original page? – Scriptable Apr 3 '15 at 17:29
  • By the way.. I dont actually wont to point the url to the actual same page, I would use a different template as its going to look different and show a smaller amount of information – Scriptable Apr 3 '15 at 17:54
  • As above — it's a possibility (and I have done such before), but it's much lower level setup comparing to typical PW functionality. – Rarst Apr 3 '15 at 19:18
0

I have managed to get this working using a rewrite, adding a query_var and doing some condiditional checking in my template file.

I will post the code below that I used but although this works and seems to be working very well, It does not feel like the best way of doing it. If someone know of a better way of doing this please post the answer.

I used this online post for reference.

Note: The listing page shows the list of hotels in the taxonomy in a side column down the side and shows the currently selected or a random one in the main content area. Which will explain how I am using the loop below.

function prefix_locations_rewrite_rule() {
    add_rewrite_rule( 'Locations/([^/]+)/([^/]+)', 'index.php?locations=$matches[1]&hotel=$matches[2]', 'top' );
}

function prefix_register_query_var( $vars ) {
    $vars[] = 'hotel';

    return $vars;
}

function prefix_url_rewrite_templates() {

    if ( get_query_var( 'hotel' ) && is_singular( 'hotel' ) ) {
        add_filter( 'template_include', function() {
            return get_template_directory() . '/taxonomy-locations.php';
        });
    }
}

add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
add_filter( 'query_vars', 'prefix_register_query_var' );
add_action( 'init', 'prefix_locations_rewrite_rule' );

In my template file for the hotels landing page:

$hotelSlug = get_query_var( 'hotel', false);

if ( have_posts() ) {
    while (have_posts()) : the_post();

        if ($post->post_name == $hotelSlug) {
            break;
        }

    endwhile;
}

This bit of code will iterate over the posts and if the hotel slug matches the query var it will break there so that the current post is the one we wanted.

We could just use a query here but as I already have a list of posts within the taxonomy I thought I'd just iterate over it. Below this I check to see if a specific hotel has been selected otherwise I show a random one from the list.

I am still to add additional logic and error handling to this code, I hope it helps someone with a similar issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.