views:

76

answers:

1

Hello, I am a bit confused about how WordPress's permalink works, especially beyond Wordpress's own usage. My permalinks are like:

%post_id%-%post_name%

But in single.php I want to put another link to page itself but with different query string. When it is clicked the permalink structure may look like:

%mystring%-%post_id%-%post_name%

I want to get the value from $_GET['action'], so:

$_GET['action'] = %mystring%

my plan is to interpret it like:

if('xx' == $_GET['action']){
   //do xx stuff
} else if ('yy'==$_GET['action']){
   //do yy stuff
} else {
   //show the single post as a single.php always shows
}

that means, I want to parse the $_GET['action'] optionally. If I do not parse it even if it is available in query string, I want the page to be rendered correctly.

So to get this done, where should I actually work? Also how do I form the link for <a> tag? Usually we make link this way:

<a href="'.the_permalink().'">TEXT</a>

but you already know, I need to add some text before the original permalink of post.

Thanks in advance.

+3  A: 

Leave your permalink structure as it was and check out my answer on custom rewrite rules.

You could adapt the code like so;

function my_rewrite_rules($rules)
{
    global $wp_rewrite;

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        '(.+)/(.+)/?$' => 'index.php?pagename=matches[2]&my_action=$matches[1]'
    );

    return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');


function my_query_vars($vars)
{
    // this value should match the rewrite rule query paramter above

    // I recommend using something more unique than 'action', as you
    // could collide with other plugins or WordPress core

    $my_vars = array('my_action');
    return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');

Now the page my_page should be available at http://example.com/whatever/my_page and http://example.com/my_page.

You can get the value of whatever using get_query_var('my_action').

Disclaimer

This may have undesired effects when viewing children pages or page attachments. You could get around this by passing an identifier in your rewrite, something to the effect of;

http://example.com/my_identifier/whatever/page

Note: You will need to edit the rewrite rule if you wish to do this. Every time you make changes to the code you will need to re-save your permalink structure to 'flush' the rules.

TheDeadMedic
Finally got this working. Thanks dude!
Kindred
+1: For disclaimer :)
shamittomar