• Resolved Pat Armstrong

    (@poisontofu)


    Hi all,

    So I currently have a hard-coded list of links to categories & sub-categories on my site, each formatted like this.

    <li><a href="/categories/interviews/" id="interviews">Interviews</a></li>

    Each category link has been given an ID which matches its slug.

    Subcategories are nested unordered lists, eg.

    <li><a href="/categories/access/" id="access">Access</a>
    	<ul class="subcategories">
    		<li><a href="/categories/access/open-access/" id="open-access">Open Access</a></li>
    		<li><a href="/categories/access/closed-access/" id="open-access">Closed Access</a></li>
    	</ul>
    </li>

    I’d like to replicate this using wp_list_categories, but am unable to assign an ID to each <a> element if I do this.

    I looked at doing this using wp_get_categories, but I need to keep the hierarchy of categories and subcategories intact, and wp_get_categories only returns a flat list. And to further complicate things, I’m using the Category Order plugin to re-order my category order, which seems to only work with wp_list_categories (correct me if I’m wrong).

    Thanks for reading –?any advice?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Pat Armstrong

    (@poisontofu)

    So I’m getting part of the way there on my own – the following code in my theme’s functions.php file allows me to add an ID to every <li> generated by wp_get_categories (this is good enough for my purposes – doesn’t need to be on each <a> element really):

    function add_id_from_slug($wp_list_categories) {
    		$pattern = '/class=/';
    		$replacement = 'id="xx" class=';
    		return preg_replace($pattern, $replacement, $wp_list_categories);
    }
    add_filter('wp_list_categories','add_id_from_slug');

    The obvious problem with this code is that I need each ID to match the category slug, which I expected this code to do for me:

    function add_id_from_slug($wp_list_categories) {
    		$pattern = '/class=/';
    		$replacement = 'id="'.$category->slug.'" class=';
    		return preg_replace($pattern, $replacement, $wp_list_categories);
    }
    add_filter('wp_list_categories','add_id_from_slug');

    But this just adds an empty ID attribute to each <li> tag. Adding something like $id = $category->slug; $output .= ' id="'.$id.'"'; to
    wp-includes/classes.php directly works, but I obviously don’t want to touch the WP core files.

    Any tips?

    Thread Starter Pat Armstrong

    (@poisontofu)

    So I realise I’m just talking to myself here, but I solved my issue in the end and thought I would add these links here incase someone else comes across this thread wanting to do something similar.

    I ended up extending Walker_Category and overwriting the default behaviour of the start_el() function that this class calls.

    These two posts were pretty key in helping me understand what a Walker is and how to extend/build on one:

    https://scribu.net/wordpress/extending-the-category-walker.html
    https://www.ads-software.com/support/topic/how-can-i-get-wp_list_pages-separated-by-commas

    Pat, I’ve been trying to figure this out, but have not had any luck. Im trying to get the output of wp_list_categories to look like this:

    <ul data-role="listview">
    	<li data-role="list-divider">PARENT-TERM</li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    	<li data-role="list-divider">PARENT-TERM</li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    	<li><a href="/child-term">CHILD-TERM</a></li>
    </ul>

    Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Customising the output of wp_list_categories’ is closed to new replies.