2

I've created a unique single post template to give people the option of reading all posts of a certain category using the awesome "reveal.js" which they access via this link

<a href="?show=presentation/">presentation</a>

The problem is the ugly URLs, which end up looking like

website.com/post-name/?show=presentation
website.com/completely-different-post-name/?show=presentation

I would like them to look like

website.com/post-name/presentation/
website.com/completely-different-post-name/presentation/

I've spent the last 10 or so hours trying to figure this out (I haven't even gone to sleep yet, lol); unfortunately, nothing has worked.

Is there a way to do this?

As a side note, the site I'm trying to do this on is installed in a subdirectory (localhost/wp) but I'm developing this for my actual website which is installed in a top level directory

1 Answer 1

2

You can achieve this via the Rewrite API's add_rewrite_endpoint:

function wpa89344_add_presentation_endpoint(){
    add_rewrite_endpoint( 'presentation', EP_PERMALINK );
}
add_action('init', 'wpa89344_add_presentation_endpoint');

Then in your template or wherever you differentiate a presentation vs normal view, check the global $wp_query for the presence of the presentation query var:

global $wp_query;
if( isset( $wp_query->query_vars['presentation'] ) ){
    include(TEMPLATEPATH . "/single_wide_report.php");
} else {
    include(TEMPLATEPATH . "/single_normal_report.php");
}

Note that you need to visit the permalinks settings page after adding the endpoint to flush permalinks and create the new rewrite rules.

EDIT- Output of $wp_query in single.php:

enter image description here

12
  • thanks Milo! Looks like I had the first part right (at one point); could you explain the second part just a bit more for me. At the moment I'm calling both templates with switch ($_GET["show"]) { case "presentation": include(TEMPLATEPATH . "/single_wide_report.php"); break; default: include(TEMPLATEPATH . "/single_normal_report.php"); break; }
    – John
    Commented Mar 4, 2013 at 18:11
  • see edit above.
    – Milo
    Commented Mar 4, 2013 at 18:30
  • is there supposed to be a semi-colon after global $wp_query? Without one, my posts go blank on me. With a semi-colon, though, I can't access the presentations template at all (even with the old ?show=presentation) Probably I'm missing something.
    – John
    Commented Mar 4, 2013 at 18:43
  • yeah, sorry, missing the semicolon. enable debugging to see php errors. have you flushed permalinks? view the $wp_query global to see if the query var is being set: print_r($wp_query);.
    – Milo
    Commented Mar 4, 2013 at 18:47
  • no worries, I'm learning lots. Everything's clear with the debug, there weren't any errors. And I'm flushing permalinks. Lastly, I added print_r($wp_query); to single_normal_report.php I'm not too sure what to look for though. Didn't see any reference to the templates
    – John
    Commented Mar 4, 2013 at 19:05

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.