At the moment my nav menu's category list only links to normal post_type archives. I'd like to dynamically append a custom post type, specifically ?post_type=portfolio, to the end of each url that particular drop down list. But I really don't know anything about php regex at the moment, except that I think that's what I need for it to work.

This is the code I've got in my functions.php file so far, specifically for the drop down list in question. As you can see, my theme appends the categories post count after the end of the link, and I'd like to keep it there please.

$folio = wp_list_categories('show_count=1&echo=0&child_of=86&title_li=<a href="http://pixelsandtea.com/category/portfolio/?post_type=portfolio" title="View all Portfolio items">Portfolio</a>');
$folio = preg_replace('/\<\/a\> \((.*)\)/',' <span>$1</span></a>',$folio);
echo $folio;



Here is something I came up with to solve this exact problem.


function example_wp_list_categories($output) {
    global $post;

    foreach (get_categories() as $cat) {
        if (preg_match("/\/category\/$cat->slug\//", $output)) {
            $output = str_replace('/category/' . $cat->slug . '/', '/category/' . $cat->slug . '/?post_type=' . $post->post_type, $output);
    return $output;

Regex isn't a strong point of mine, so that might be improved upon.

| improve this answer | |

i am little confuse, you want to append post_type in category? category is a taxonomy? post_type is something different.

if i am not mistaken, you want to create dropdown for a custom post_type taxonomy? if so, use the following code

<? wp_dropdown_categories(array('hide_empty' => 0, 'taxonomy' => 'chefs''name' => 'select_name', 'hierarchical' => true)); ?>

replace 'chef' with your taxonomy, by default 'category' is used for post_type = post

| improve this answer | |
  • I need it to be a drop down, nested unordered list, like I'm using in the header of my site - pixelsandtea.com - at the moment. I'm using the wp_list_categories function to list those categories (which all contain only the custom post_type=portfolio) as part of my nav menu. But for example, at the moment the PRINT CATEGORY's href is targeting pixelsandtea.com/category/print however I need it to target pixelsandtea.com/category/print/?post_type=portfolio. If you still can't work out what I mean take a look at the portfolio section of my nav menu. – Ben Oct 22 '11 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.