0

I've been trying to adapt the rewrite functions described here with little success:

The structure of the URL I'd like to achieve is

/academics/faculty/profile/faculty-member

Where /academics is the blog, faculty is the page, and profile is the child page of profile. If you use this:

/academics/faculty/profile?fid=faculty-member

It displays the faculty profile, no problem. What I want is to assign fid as a rewrite that cleans up the variable from the URL and leaves only the faculty member.

I've rewritten the function so that the add_rewrite_rule looks like this:

// Register the variables that will be used as parameters on the url
function add_my_var($public_query_vars) {
    $public_query_vars[] = 'fid';
    return $public_query_vars;
}
add_filter('query_vars', 'add_my_var');

// Build the rewrite rules, for the extra parameter
function do_rewrite() {
    add_rewrite_rule('(faculty/profile)/[/]?([^/]*)$', 'index.php?pagename=profile&fid=$matches[1]','top');
}
add_action('init', 'do_rewrite');

// The parameter is now accessible
get_query_var('fid') ;

I thought I was following the directions accurately, and I've flushed the permalink structure, but when I try to use the new URL it just reverts to /academics/faculty/profile and spits nothing out, even when I add:

$profile2 =     get_query_var('fid') ;

to the page template.

Is my rewrite accurate? Any idea why it would work in the prior examples but not here?

1

When using the pagename query var with a parent/child page, the value should reflect the parent/child relationship, or it will cause a redirect. Instead of:

pagename=profile

try:

pagename=faculty/profile

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.