apply_filters( ‘category_link’, string $termlink, int $term_id )

Filters the category link.

Parameters

$termlinkstring
Category link URL.
$term_idint
Term ID.

Source

$termlink = apply_filters( 'category_link', $termlink, $term->term_id );

Changelog

VersionDescription
5.4.1Restored (un-deprecated).
2.5.0Deprecated in favor of 'term_link' filter.
1.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    WordPress hooks provide a powerful way to modify and extend the core functionality of your site. The apply_filters() function is used to apply filters to a variable, allowing you to modify data before it is used. The category_link filter specifically allows you to alter the URL of a category link before it is displayed.

    Simple Examples

    function wpdocs_category_link_filter( $termlink ) {
        // Modify the term link as needed
        $termlink .= '?custom_param=1';
    
        return $termlink;
    }
    
    add_filter( 'category_link', 'wpdocs_category_link_filter' );

    Complex Examples

    function wpdocs_category_link_filter( $termlink, $term_id ) {
        // Modify the term link only for category with ID 5
        if ( 5 === $term_id ) {
            $termlink = home_url( '/special-category/' );
        }
    
        return $termlink;
    }
    
    add_filter( 'category_link', 'wpdocs_category_link_filter', 10, 2 );

    By using apply_filters() with the category_link filter, you can easily customize the URLs of category links in WordPress. This technique allows you to tailor category links to meet your specific needs.

You must log in before being able to contribute a note or feedback.