I am creating two versions of pages, one in Spanish and the other in English.
To prevent him from breadcrumbs
generating the categories like this:
Home » En » Beauty Products
By means array
of I can change the name of certain parameters of the URL
by another name, in the case of, /belleza/
as you can see, the name is changed to Productos de Belleza
, yes, the generated link does keep its current link, which in this case it would be example.com/es/belleza
and that's fine.
<?php
$trail = array(
'es' => '',
'belleza' => 'Productos de Belleza'
);
$url = '/es/belleza/perfume-de-mujer ';
$parts = parse_url($url);
$path = pathinfo($parts['path']);
$segments = explode('/', trim($path['dirname'],'/'));
$breadcrumbs[] = '<a href="/">Home</a>';
$crumb_path = '';
foreach ($segments as $segment) {
$crumb_path .= '/' . $segment;
$value = (array_key_exists($segment, $trail)) ? $trail[$segment] : ucfirst($segment);
$breadcrumbs[] = '<a href="' . $crumb_path . '">' . ucwords(str_replace(['_', '-', '+'], ' ', $value)) . '</a>';
}
$breadcrumbs[] = ucwords(str_replace(['_', '-', '+'], ' ',
$path['filename']));
//$breadcrumbs = implode(' » ', $breadcrumbs);
array_pop($breadcrumbs);
$breadcrumbs = implode(' » ', $breadcrumbs);
echo $breadcrumbs;
?>
Now I want to prevent the link from being generated if I array
change this value: es
to an empty value ''
.
$trail = array(
'es' => '',
'belleza' => 'Productos de Belleza'
);
But it didn't work as I expected, it generated this:
<a href="/">Home</a> » <a href="/es"></a> » <a href="/es/belleza">Productos De Belleza</a>
which is equal to:
Home » » Beauty Products
So what I want is the following if I replace a URL value with other data, in the same way as I do between this /beauty/ for this: Productos De Belleza
but in this case if I leave it empty like this: 'es' => '',
no I generated that data, no link, no ».
The result I expected was the following: How can I get this result?
All you need to achieve what you want is to add a conditional that checks if the value is empty or not. If
$value
it's empty, don't insert it into the breadcrumbs. That way you won't put links without text.With that simple change, the code would look like this and you would get the expected result: