1

In my theme i have an archive template for a taxonomy (taxonomy-cat_projet.php), in this template a have a list of these taxonomies links to allow users to select a taxonomy. Here is the code i'm using to list categories links:

<ul>
    <?php  $args = array( 
            'title_li' => '',
            'taxonomy' => 'cat_projet',
            'hide_empty' => false,
    );
    wp_list_categories( $args ); ?>
</ul>

Is there a way to once a category is selected, its link would change so that if it's clicked it lists posts from all categories. like, first click makes that category active, and the second time it's clicked it becomes inactive.

I know i could do it in javascript, but i prefer doing it in php, like using some "current taxonomy link" filter if it exists.

1

Internally wp_list_categories() uses get_term_link() for the URL of the terms. That function can be filtered using the term_link filter, so you could filter any links to the current term and replace them with links to the post type archive:

function wpse_307202_term_link( $termlink, $term, $taxonomy ) {
    if ( is_tax( 'cat_projet' ) ) {
        if ( get_queried_object_id() === $term->term_id ) {
            $termlink = get_post_type_archive_link( 'post_type_name' );
        }
    }

    return $termlink;
}
add_filter( 'term_link', 'wpse_307202_term_link', 10, 3 );

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.