• I’ve tried searching for this but haven’t found quite what I need. Currently wp_list_categories(); will output LIs with class names:
    <li class="cat-item cat-item-3">

    But I ideally would like to grab the category slug and use it as an ID like this:
    <li id="category_slug">

    Is this possible?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Is this possible?

    Not without modifying the core code, no.

    Nor is it really necessary. What exactly is it that you need that id for?

    The list items have the category ID <i>number</i>, that should be all you need for specific styling with CSS. It’s easy enough to filter the list to add the IDs though. Just add this to your functions.php file.

    // Register filter so we can edit the list generated by wp_list_categories
    add_filter('wp_list_categories', 'wp_list_categories_add_ids');
    
    function wp_list_categories_add_ids($content) {
    	// Use a regular expression to match the list item (and important bits like
    	// the title) and pass it to a function which will return the replacement
    	$content = preg_replace_callback('#<li(.*?)>(.*?)</li>#is', 'wp_list_categories_add_ids_callback', $content);
    	return $content;
    }
    
    function wp_list_categories_add_ids_callback($matches) {
    	// Build the replacement list item using the matches from the expression
    	$replacement  = '<li' . $matches[1] . ' id="' . sanitize_title_with_dashes($matches[2]) . '">';
    	$replacement .= $matches[2];
    	$replacement .= '</li>';
    	return $replacement;
    }
    Thread Starter jonboldendesign

    (@jonboldendesign)

    Nor is it really necessary. What exactly is it that you need that id for?

    It’s just more semantic and cleaner to have an id with words than the number. I would prefer that.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    It’s just more semantic and cleaner to have an id with words than the number. I would prefer that.

    The problem is that the name of a category (which the code above uses) is not guaranteed to be unique, and so is potentially unsuitable for an ID. It could be okay for a class, but the uniqueness problem would still exist and make styling potentially difficult.

    The database’s id number may not be perfectly semantic, but it does allow tying the category entry in the database to a specific class for styling purposes.

    Also, it’s not correct to use ID’s on the output from these functions in any way at all, as they are not guaranteed to only be used once on the page. I could easily have two different lists of categories displayed differently. Using a class is safer. The slug would indeed be fine as it is unique, but only as a class, not as an ID.

    Otto is right. Also, my code is kind of silly unless you want to allow other filters to modify the wp_list_categories output.

    If you want more control over the HTML, use something like this:

    <ul>
    	<?php
    		$categories = get_categories();
    		foreach($categories as $category) {
    	?>
    		<li class='<?php echo $category->sulug; ?>'>
    			<a href='<?php echo get_category_link($category->cat_ID); ?>'><?php echo $category->name; ?></a>
    		</li>
    	<?php
    		}
    	?>
    </ul>

    Also, get_categories can take the same parameters as wp_list_categories.

    Thread Starter jonboldendesign

    (@jonboldendesign)

    It’s not really easy to explain why I want to do it this way. It’s totally correct to use an ID on something that you only use once. I would understand avoiding it if I were creating something for lots of different users / sites, but this is a one-time project. For example if I’m having a list and I want each rollover to be a different color.

    #menu ul li#about-us{
    
    #menu ul li#affiliates{

    There’s nothing incorrect about that.
    Thanks for your help everyone.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Use Category Slug as link ID?’ is closed to new replies.