is_post_type_archive not working in feed
-
21-04-2021 - |
Pergunta
I'm trying to alter a feed for my plugin's custom post type to add lines for podcasting. Here is the code I've got right now:
add_action ('init', 'my_plugin_init');
function my_plugin_init() {
if ( is_post_type_archive('my_plugin_custom_type') ) {
add_action('rss2_ns', 'my_plugin_podcast_ns');
}
}
function my_plugin_podcast_ns() {
echo 'xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"';
}
The archive for this post type is located at http://example.com/custom_type/
, and I'm getting the feed at http://example.com/custom_type/feed/
. However, my extra line is not showing up. What am I doing wrong?
Solução
'init'
is probably too early to get a meaningful result from is_post_type_archive()
which depends on the the query having been run. 'template_redirect'
is probably the earliest action you could run it.
Outras dicas
On WordPress 5.x+ : The 'wp'
action works as a place to catch query dependent conditions. For example is_post_type_archive()
as a condition successfully and then set the cookie. 'pre_get_posts'
at least in my case did not work. (source)
e.g.:
add_action( 'wp', 'set_archive_cookie');
From Codex: More about actions. More about the 'wp'
action.