0

I have recently faced multiple issues with a theme developed with in mind pretty permalinks when moving back to ugly ones (for a number of different reasons). Had issues that I resolved with categories and tags as displayed on my sidebar and also now have the same issue with categories in a custom navigation menu.

Originally, the below code would take me to all the posts whose category is called ‘Amazing’, currently with the use of ugly permalinks (to which I need to stick now for a bunch of reasons) I am returned with a 404 error message. Here is an excerpt of my navigation:

<nav class="my-nav-menu">
  <div class="myclass">
    <ul role="navigation" class="mymenuentries">
      <li class="col-clas">
         <a class="myotherclass" href="<?php echo home_url() ?>/category/Amazing">AmazingCategory</a>
      </li>
   </ul>
  </div>
</nav>

The code will generate this URL:

http://localhost/mywebsite/Amazing

that returns a 404 message, whereas I know that the following will return all the posts associated to that category:

http://localhost/mywebsite/?cat=12

How can I dynamically make sure that the category is fetched by its ID? I am also very much not aware how that piece of code was actually working with pretty permalinks. Can you help?

Thank you

  • So you have hard-coded links that you want to replace? I think the answer may be the same as the one to your other question. – Milo Feb 26 '17 at 1:25
  • Yeah, it is long story, the theme was developed for and worked fine with pretty permalinks, but it is not fine with ugly ones. I have tried the same approach by doing this: <a class="myotherclass='". get_term_link( $sectionSlug) . "'>AmazingCategory</a> but I am getting the wrong category name and id. Please note that my sub-nav is a separate template php than my sidebar.php. I am very much lost as I had no idea why it was actually working fine with pretty permalinks. Any idea how I can change it to pick the right cat/cat id? Thanks – paranza Feb 26 '17 at 1:33
1

Use get_term_link with the slug, and specify the taxonomy:

$cat_slug = 'Amazing';
$taxonomy = 'category';
$cat_link = get_term_link( $cat_slug, $taxonomy );

if ( is_wp_error( $cat_link ) ) {
   echo $cat_link->get_error_message();
} else {
    echo $cat_link;
}
  • This is brilliant, thank you - it works beautifully. – paranza Feb 26 '17 at 15:59

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.