• Resolved nagomitaro

    (@nagomitaro)


    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:

    https://localhost/mywebsite/Amazing

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

    https://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

    • This topic was modified 8 years ago by nagomitaro.
Viewing 7 replies - 1 through 7 (of 7 total)
  • swayam.tejwani

    (@swayamtejwani)

    Hi Nagomitaro,

    As the category link is hard coded by name, why not just replace that with ?cat=12 permalink? So, instead of this

    
    href="<?php echo home_url() ?>/category/Amazing"
    

    Replace it with

    
    href="<?php echo home_url() ?>/?cat=12"
    
    Thread Starter nagomitaro

    (@nagomitaro)

    Hi,
    Thank you for your reply. This is a good shout and tried it already, but I wanted to have a more dynamic way to retrieve the category ID rather than encoding it again via its ID. I have this:

    $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;
    }

    and I am trying to do something like this:

    <nav class="my-nav-menu">
      <div class="myclass">
        <ul role="navigation" class="mymenuentries">
          <li class="col-clas">
             <a class="myotherclass" href=". get_term_link($cat_link) . "'>AmazingCategory</a>
          </li>
       </ul>
      </div>
    </nav>

    But I cannot get it to work. Where do I call the php code? Just before the nav class? is the get_term_link usage correct?

    Thank you

    Thread Starter nagomitaro

    (@nagomitaro)

    Just realised that I only need to call $cat_link inside href. I have tried this:

    <li class="col-clas">
                <a class="myotherclass" href='{$cat_link}'>News.</a>
              </li>

    But that is not correct. Should I include the php code inside a function?

    Thank you

    swayam.tejwani

    (@swayamtejwani)

    Ok, it will be then something like this

    
    <a class="myotherclass" href="<?php echo $cat_link; ?>">News.</a>
    

    haven’t tried, but it should work.

    Thread Starter nagomitaro

    (@nagomitaro)

    Hi,

    Thank you so much, it works fine. This is what I have ended up with:

    <nav class="my-nav-menu">
      <div class="myclass">
        <ul role="navigation" class="mymenuentries">
          <li class="col-clas">
             <a class="myotherclass" href="<?php $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;
    }?>">AmazingCategory.</a>
          </li>
       </ul>
      </div>
    </nav>

    I have one additional question, in case I have more than one category, how can I make that more generic?

    Thank you

    swayam.tejwani

    (@swayamtejwani)

    For getting more then one category, you can use this function “get_categories”

    Thread Starter nagomitaro

    (@nagomitaro)

    Brilliant, thank you so much for your help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do I retrieve the category ID (ugly permalinks) in my sub-navigation menu?’ is closed to new replies.