WordPress.org

Ready to get started?Download WordPress

Forums

retrieving cat ID via cat slug (5 posts)

  1. Dunkkan
    Member
    Posted 4 years ago #

    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.

  2. MichaelH
    Member
    Posted 4 years ago #

    This uses category name:

    $category = get_cat_ID( 'My Cat');

    or

    $category = get_term_by( 'slug', 'cat1', 'category' );
  3. Dunkkan
    Member
    Posted 4 years ago #

    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 ?

  4. MichaelH
    Member
    Posted 4 years ago #

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

    Nope!

  5. maorb
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic