Tuesday, 22 October 2019

Breadcrumb menu duplicates link when query string is parsed

I've created a breadcrumb menu which get's the page URL, explodes the text after each trailing slash (after the domain) and prints it out.

This works fine if no query string is added to the URL, however, once a query is added, it duplicates the link:

Working example:

<?php
    $url = "/parent/child";
    // 1. Get URL
    $crumbs = explode("/", trim($url, '/'));
    // 2. Strip extras
    $build = '';
    // 3. Define int so last item is not a link
    $lastKey = count($crumbs) - 1;
    // 4. Execute loop
    foreach($crumbs as $key => $crumb){
        $build .= '/'.$crumb;
        // format text 
        $crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
        $crumb = preg_replace('/-/', ' ', $crumb); // remove dashes
        $crumb = trim($crumb); // remove whitespace from before and after string

        $pagename = "child";  

        echo $key < $lastKey
            ? "$crumb /" : $pagename;
    }
?>

The above outputs: Parent / child

Which is fine.

However, now the second test with a query string (notice the $url):

<?php
    $url = "/parent/child/?=query-string";
    // 1. Get URL
    $crumbs = explode("/", trim($url, '/'));
    // 2. Strip extras
    $build = '';
    // 3. Define int so last item is not a link
    $lastKey = count($crumbs) - 1;
    // 4. Execute loop
    foreach($crumbs as $key => $crumb){
        $build .= '/'.$crumb;
        // format text 
        $crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
        $crumb = preg_replace('/-/', ' ', $crumb); // remove dashes
        $crumb = trim($crumb); // remove whitespace from before and after string

        $pagename = "parent";  

        echo $key < $lastKey
            ? "$crumb   /" : $pagename;
    }
?>

This outputs: Parent / Child / parent Expected output: **Parent / Child **

I understand that I've set $pagename = "parent", but I want it to ignore query strings.

I've tried the following:

$crumbs = explode("/", trim($_SERVER["REQUEST_URI"], '/'));
$crumbs = preg_replace('/\?.*/', '', $crumbs);

Still same results.

Latest code:

<?php

    // 1. Get current URL
    $crumbs = trim($_SERVER["REQUEST_URI"]);
    $crumbs = preg_replace('/\?.*/', '', $crumbs);
    $crumbs = explode("/", $crumbs);
    array_filter($crumbs);


    $count = count($crumbs);
    $build = '';

    // 3. Define int so last item is not a link
    $lastKey = count($crumbs) - 1;

    // 4. Execute loop
    foreach($crumbs as $key => $crumb){
        $build .= $crumb;

        // format text 
        $crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
        $crumb = preg_replace('/-/', ' ', $crumb); // remove dashes
        $crumb = trim($crumb); // remove whitespace from before and after string

        $pagename = get_query_var('pagename');  
        $pagename = the_title('', '', false); // print page name as is in WP
        $pagename = preg_replace('/-/', ' ', $pagename); // remove dashes
        $pagename = trim($pagename);

        echo $key < $lastKey
            ? "<a class='crumbLink' href=".$build.">".$crumb."</a>
               <span class='slash'>/</span>"
            : $pagename;

    }


?>


from Breadcrumb menu duplicates link when query string is parsed

No comments:

Post a Comment