0

I have a regular categories widget (or "block" in the new terminology), that creates a <ul> with my categories.

enter image description here

I want to add a fake category for linkbuilding reasons, an entry to this list that points to an external URL.

How can I do?

I found only the_category_list hook but it doesn't seem to do what I need...

Tried also with the filters widget_categories_args and block_categories_all and but they are not called. (wp 6.0)

1
  • 2
    the_category_list filters the list of categories on a given post, eg, for display on a single post. I think you're looking for the get_terms filter, which filters the list of terms in a given taxonomy (in your case, the taxonomy is category).
    – Pat J
    Jun 27, 2022 at 13:43

1 Answer 1

0

Done like this:

function cat_filter($args) {

    if (count($args) < 10) {
        return $args;
    }

    $new = new WP_Term((object)[
        'term_id' => -17,
        'name' => 'Blog',
        'slug' => 'blog',
        'taxonomy' => 'category',
    ]);
    array_splice($args, -5, 0, [$new]);

    return $args;
}

add_filter('get_terms', 'cat_filter', 10, 1);

add_filter('term_link', function( $url, $term, $taxonomy ) {
    if ( $term->term_id == -17 ) {
        $url = "https://example.com";
    }
    return $url;
}, 10, 3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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