• Resolved laz0rama

    (@laz0rama)


    i am completely stumped on this, i’m hoping i am missing something obvious. here is the situation:

    i have a page in the database with the slug ‘banks’. in the page.php file of my theme, i have the following:

    if (str_starts_with($_SERVER[‘REQUEST_URI’], ‘/banks/’))
    {
    require(TEMPLATEPATH . ‘/page-templates/page-template-institutions.php’);
    exit;
    }

    switch($_SERVER[‘REQUEST_URI’])
    {
    case ‘/banks’:
    require(TEMPLATEPATH . ‘/page-templates/page-template-institutions.php’);
    exit;

    }

    in page-template-institutions.php i generate the content for the requested page, based on the actual url (eg, /banks or /banks/bank-name). this works flawlessly, both for the urls ‘/banks’ and ‘/banks/bank-name’.

    then i added another page in the database with the slug ‘credit-unions’. i added the same logic to page.php to handle relevant urls:

    if (str_starts_with($_SERVER[‘REQUEST_URI’], ‘/credit-unions/’))
    {
    require(TEMPLATEPATH . ‘/page-templates/page-template-institutions.php’);
    exit;
    }

    switch($_SERVER[‘REQUEST_URI’])
    {
    case ‘/credit-unions’:
    require(TEMPLATEPATH . ‘/page-templates/page-template-institutions.php’);
    exit;

    }

    when i request the url ‘/credit-unions’, it works perfectly, and i get the generated content. however, when i request a url like ‘/credit-unions/credit-union-name’, i get a 404 page not found.

    the logic seems to be identical between banks and credit-unions, but for some reason wordpress is doing something different with urls that begin with ‘/credit-unions/…’ than it is with urls that begin with ‘/banks/…’. i cannot find anywhere in my code that would account for that. am i missing something in this scenario?

    any useful ideas GREATLY appreciated. thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter laz0rama

    (@laz0rama)

    i think i just found a workaround, if not an actual solution. i still don’t know why wordpress is treating the urls differently, but i am able to add this to my theme’s functions.php file to get me where i need to be:

    add_action(‘parse_request’, ‘custom_url’);
    function custom_url()
    {
    if (str_starts_with($_SERVER[‘REQUEST_URI’], ‘/credit-unions/’))
    {
    require(TEMPLATEPATH . ‘/page-templates/page-template-institutions.php’);
    exit;
    }
    }

    Moderator bcworkz

    (@bcworkz)

    Without a much more complete understanding of your site’s data schema, it’d be nearly impossible to identify why /banks/blah/ works but not /credit-unions/blah2/.

    From the little information you’ve offered it seems like you’ve departed from the way WP would normally deal with your situation. It’s not wrong per se, just different. If you have it all working as desired there’s no reason I can see to do anything different.

    If you were to start over (I’m not suggesting you do or would), there’s likely a more orthodox way to set things up. However… “If it works don’t fix it.” ??

    Thread Starter laz0rama

    (@laz0rama)

    thanks for the response. i too am a big fan of “if it ain’t broke, don’t fix it”, so i will stick with the workaround i put in place. ??

    • This reply was modified 8 months, 4 weeks ago by laz0rama.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘404 on custom pages’ is closed to new replies.