WordPress.org

Support

Support » Plugins and Hacks » retrieving cat ID via cat slug

retrieving cat ID via cat slug

  • Hi guys,

    I’m crafting a theme for public release.

    I’d like to list, in the main menu, up to 7 categories, that the user would choose between those he has already prepared by his own, assigning from the Admin Panel some new slugs to his already set up cats.

    The problem is, that wp_list_categories is asking me for some numeric IDs, which me, as a themer, I ingore because they will depend on every single WP install, so I can’t use such an idea.

    Do you know if it’s possible to code some PHP, turning a slug (known, coded in the template, communicated to the user in the instructions) into a numeric ID (unknown, used to print a link) ?

    This, simpler in its focus, was giving me the printed word ‘Array’. 🙂

    <?php
    $category = get_the_category('category_nicename=cat1');
    echo $category ;
    ?>

    Thanks for any input.

Viewing 4 replies - 1 through 4 (of 4 total)
  • This uses category name:

    $category = get_cat_ID( 'My Cat');

    or

    $category = get_term_by( 'slug', 'cat1', 'category' );

    Thanks !

    Here’s the code I finished with :

    <?php
    		$category = get_cat_ID( 'Custom1');
    
    		$params = array(
    		'title_li' => '',
    		'include'   => $category );
    
    		wp_list_categories($params);
     ?>

    The rest is some basic HTML and CSS to create an horizontal navbar, and the code repeats for each div, changing the custom# category.

    Thanks Michael for that one.

    Do you think there’s a better code to pass the params/print them ?

    Do you think there’s a better code to pass the params/print them ?

    Nope!

    Why not adding to the get_cat_ID() function the option to pass also slug as the parameter, and not only by category name?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘retrieving cat ID via cat slug’ is closed to new replies.