0

So, I am using this to get the URL:

<a href="/category/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ' '; } ?>">

Which is outputting:

http://example.com/category/whatever%20category/

instead of:

http://example.com/category/whatever-category/

How can I make it do the latter?

Much thanks.

  • Accept the answer that worked for you. :) – Shreyo Gi Jan 28 '15 at 6:25
2

Try this

<a href="/category/<?php foreach((get_the_category()) as $category) { echo $category->slug . ' '; } ?>">

Hope this helps.

  • Beaten me by seconds :-). This is correct +1 – Pieter Goosen Jan 28 '15 at 5:54
  • :) It happens @PieterGoosen. Anyways, thumbs up for your code! – Shreyo Gi Jan 28 '15 at 5:56
  • This seems to be the closest I've got! Now it is outputting the text like this: random-category (instead of Random Category). Using it like this: <a href="/category/<?php foreach((get_the_category()) as $category) { echo $category->slug . ' '; } ?>"><?php foreach((get_the_category()) as $category) { echo $category->slug . ' '; } ?></a> – lorenzium Jan 28 '15 at 6:33
  • @lorenzium let me get clarified. You want something like the below? <a href="/category/random-category">Random Category</a> or <a href="/category/Random Category">Random Category</a> – Shreyo Gi Jan 28 '15 at 6:39
  • @ShreyoGi - yes, the first option: <a href="/category/random-category">Random Category</a> – lorenzium Jan 28 '15 at 9:23
2

I think it will be easier to make use of the category slug, and not the name. It is already in lowercase and hyphen separated if you have more that one word.

The problem with using the name and adding hyphens is that you will not get a match and most probably just 404. %20 is how blank spaces is encoded in a URL.

You can just replace $category->cat_name with $category->slug

Just for interest sake, if you have to use the name and replace blank spaces with a hyphen, you can use str_replace()

$hyphened_name = str_replace( ' ', '-', $category->cat_name );
0

Change this: echo $category->cat_name

To this: echo $category->cat_slug

A better way would probably be to use:

$cat = get_the_category();
$cat_link = get_term_link( $cat );

That will have $cat_link as the full permalink.

echo '<a href="' . $cat_link . '">' . $cat->cat_name . '</a>';

Using your code above:

<a href="<?php
    $cat = get_the_category();
    echo get_term_link( $cat );
?>">

Personally, I try to avoid intermixing things like that as it gets to be a pain to read, but the above should replace what you have.

Better would be

<?php
   $cat = get_the_category();
   $cat_link = get_term_link( $cat );

   echo <<<HTML
<a href="{$cat_link}">{$cat->cat_name}</a>
HTML;

?>

That would also replace what you wrote.

  • Thanks for the answer but I can't seem to get it to work. It is outputting the name but not the link. How would I use this to replace my code above? – lorenzium Jan 28 '15 at 1:51
  • I added two optional replacements to what you wrote. – Privateer Jan 28 '15 at 2:13
  • None of these worked I'm afraid. – lorenzium Jan 28 '15 at 10:11

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.