Direct link to category?

I’m trying to build up a menu of category links for specific categories I’d like to highlight and apply styling/classes to.

I’ve tried this sort of thing.

Read More
<li><a href="<?php get_site_url(); ?>category/Gadgets"> Gadgets</a></li>

And

 <li><a href="category/Gadgets"> Gadgets</a></li>

It’s showing up as sitename/category/category/category/gadgets on the front end.

I can’t use <?php wp_list_categories('orderby=name&title_li='); ?> because it’s going to spit out all of them and limit the styling I can do on the html.

I just want to link to categories directly, by name, preferably without hard coding in the site name.

Related posts

4 comments

  1. There is a function called get_category_link() which might be helpful for you. This will be able to generate an appropriate link without having to hard-code it, except for the category name or ID.

    Examples from the WordPress Codex:

    <?php
        // Get the ID of a given category
        $category_id = get_cat_ID( 'Category Name' );
    
        // Get the URL of this category
        $category_link = get_category_link( $category_id );
    ?>
    
    <!-- Print a link to this category -->
    <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>
    

    http://codex.wordpress.org/Function_Reference/get_category_link

  2. Specific to your demand, try this:

    <li><a href="<?php echo home_url() ?>/category/Gadgets"> Gadgets</a></li>
    
  3. use wp_list_categories('include=3,5,9,16');, replace “3,5,9,16” with your category ids ( comma separated

    another method

    get_category_link( $category_id );
    

    see wordpress codex here

    One more method is to create category menu in admin and print it in your theme ( if theme supports menus )

Comments are closed.