• Resolved formica

    (@formica)


    Hi there

    I just need to get a variable in the query string using wp_list_categories then onto the taxonomy template where I’ll display different content accordingly.

    I have this code from the codex

    But how do i get it to output the same link but with ‘?area=map’ at the end?

    Probably quite straight forward but coming up blank.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • Short answer: You can’t.

    Longer answer: You’d need to build your own category listing function – perhaps using get_categories().

    Thread Starter formica

    (@formica)

    Hmm… I’m listing the taxonomies of a custom post type. So not sure it would work. Could you use preg_replace just to tack it onto the end?

    Found this which adds an ‘id=xx’ to the ‘li’ but doesn’t alter the url.

    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');

    Do you know if it could be altered to add to the the url? Otherwise it’s the dreaded walker class for me.

    Thanks.

    Thread Starter formica

    (@formica)

    Aha! so got it working using a filter.

    function filter_categories($output, $args=array()){
          return preg_replace('/(\<a\shref=\"?[^\>]+?)\"/', '$1?area=map"', $output);
    }
    add_filter('wp_list_categories', 'filter_categories', 10, 2);

    outputs: https://www.mysite.com/area/central/?id=map

    Last hurdle. How do I get it to add that filter only from a certain page.

    I need to get if is_page in there but it won’t work, probably due to my bad php.

    Any pointers appreciated.

    Thanks

    Thread Starter formica

    (@formica)

    No worries got it working using ‘$id = get_the_title();’ then on the next page I’ll use a simple get_id and if, else to display different content. Quite static really, but works for what i need. final code:

    function filter_categories($output, $args=array()) {
    	$id = get_the_title();
    	return preg_replace('/(\<a\shref=\"?[^\>]+?)\"/', '$1?id='. $id .'"', $output);
    		}
    	add_filter('wp_list_categories', 'filter_categories', 10, 2);
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Add viaraible to wp_list_categories’ is closed to new replies.