Using WordPress ‘get_category_feed_link()’ PHP function

The get_category_feed_link() WordPress PHP function retrieves the feed link for a category.

Usage

get_category_feed_link($cat, $feed = '');

Parameters

  • $cat (int|WP_Term|object) – The ID or category object whose feed link will be retrieved.
  • $feed (string, Optional) – Feed type. Possible values include ‘rss2’, ‘atom’. Default is the value of get_default_feed().

More information

See WordPress Developer Resources: get_category_feed_link()

Examples

Basic usage

Retrieve the RSS2 feed link for posts in category 2.

echo get_category_feed_link(2, '');

Display an RSS link automatically when viewing a category. Insert this code on the category.php or archive.php page template.

if (is_category()) {
    $category = get_category(get_query_var('cat'));
    if (!empty($category)) {
        echo '<div class="category-feed"><a href="' . esc_url(get_category_feed_link($category->cat_ID)) . '" title="' . sprintf(esc_attr__('Subscribe to this category', 'textdomain'), $category->name) . '" rel="nofollow">' . __('Subscribe!', 'txtdomain') . '</a></div>';
    }
}

Retrieve the Atom feed link for posts in category 3.

echo get_category_feed_link(3, 'atom');

Retrieve the feed link using a WP_Term object.

$term = get_term(4, 'category');
echo get_category_feed_link($term);

Retrieve the feed link using a category object.

$category_obj = get_category(5);
echo get_category_feed_link($category_obj);