0

Im currently facing an issue with the relationship between pretty permalink and my themes templates.

I have a post type called 'performance', and with pretty permalinks enabled the URLs for the template at single-performance.php looks like this:

www.my-website.com/performance/name-of-performance

In the sidebar of that template, I am listing 5 other performances. This all works fine so far. Standard WordPress stuff.

On another page I am listing all the performances that plays during school hours. This is done with a WP_Query filtering with a meta_query. Still fine.

But this is where I get stuck: When you click one of the school performances it links to the template single-performance.php, and this template has 5 other performances listed in the sidebar. But these are not filtered by school hours. They just show the normal 5 performances.

Is it possible to have an optional parameter in the url so that if it looks like this:

www.my-website.com/performance/name-of-performance

... it lists the normal 5 performances in the sidebar. But if it looks like this:

www.my-website.com/performance/schools/name-of-performance 

... it lists 5 performances filtered by the school hours.

In a world without permalink I would simple have this url

www.my-website.com/page_id=12&schools=1

Then in the template i would go

if( isset($_GET['schools'] ) && $_GET['schools'] == 1 ) {
  // Show performances filtered by school hours in sidebar
} else {
  // Show 5 performances without filtering them
}

Please let me know if this is possible, and if it is, what approach to take.

I have been looking at add_permastruct() add_query_vars filter and add_rewrite_rules() but it all confuses me, as most examples are not solving what I am trying to solve.

2
  • it's generally a bad idea to have the same content visible from multiple URLs. another option is to combine both strategies, making your URL /performance/name-of-performance/?schools=1
    – Milo
    Commented Jul 15, 2015 at 16:31
  • Is that better in terms of duplicate content? Not as pretty though? Even if it is duplicate - is it possible?
    – Malibur
    Commented Jul 15, 2015 at 19:58

1 Answer 1

1

Update

You can make use of the add_rewrite_endpoint:

add_rewrite_endpoint( 'schools', EP_PERMALINK | EP_PAGES );

You can then check in the sidebar for the schools variable:

global $wp_query;

if ( isset( $wp_query->query_vars['schools'] ) ) {
    // ...
}

What is worth mentioning is that your URL will look like www.example.com/performance/name-of-performance/schools/ instead of having schools immediately after the custom post type's name.

4
  • The reason I dont think that would work, is that I need the sidebar to be different depending on context. That means, if you get there via one page, you get sidebare1 if you get there via another (the school page) you get sidebar2. But the content on the page and the template is the same. So I need to differentiate somehow - preferably via the URL.
    – Malibur
    Commented Jul 15, 2015 at 17:16
  • I am sorry, is there something that I am missing? You have mentioned you use a custom WP_Query to show the school hours performances posts and that you want a different sidebar content (posts / performances) that match the current post condition of being during school hours or not. Is there more to it?
    – webtoure
    Commented Jul 15, 2015 at 17:20
  • I am sorry, maybe im not explaining it very well. I want the same post (performance) to be able to show with two different sidebars depending on the context. If I simply show a sidebar based on a term, then the sidebar will always be one or the other. I'd like to show the school-sidebar if the word school is in the url, and a normal sidebar if school is not. But both situations should be applicable to the same post at all times. Does that clarify it?
    – Malibur
    Commented Jul 15, 2015 at 19:57
  • That works! thank you. This might even be a better structure for my use case, though im still interested to know if it would be possible to do it with /schools/post-name as a structure. Note that I edited the key in query_vars, as that was probably just a copy-paste error. Thank you for helping.
    – Malibur
    Commented Jul 16, 2015 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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