Access category within rss2_head hook?

The question:

Can I access the current category of a category feed with the rss2_head hook to add for example itunes tags?

Lets say I have wordpress.com/catx/feed I want to get acf field elements associated with this specific category.

Here is what I am trying to accomplish:

function itunes_head() {
        $category = get_the_category();
        $categories = get_category();
        global $post;
        var_dump($categories);
        echo print_r($post);
        echo $categories;
        echo $category;
}
add_filter( 'rss2_head', 'itunes_head' );

I am assuming that I somehow can retrieve the catx category here?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I think you can just fetch it from the global query using get_queried_object:

function itunes_head() {
    if ( is_category() ) {
        $category = get_queried_object();

        if ( isset( $category ) ) {
            $acf_category = 'category_' . $category->term_id;

            $field = get_field( 'my_category_field', $acf_category );
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment