How can i use this conditional in a function?
When i write this code it doesnt work:
Function test(){
if(is_tag()){
echo 'something';
}else{
echo 'somthing else';
}
}
add_action('init','test');
Because at init, you’re not hooking anything useful.
What is it you’re actually trying to accomplish.
The is_tag() function will not work with the init hook because the main query has not yet been done. Try this hook instead:
add_action('parse_query','test');
Does that work in the way you want?
I did this in your way and it correct way
but there is something wrong with it
when I wrote this code it gave me about 6,7 “something” or “something else”
function test(){
if(is_tag()){
echo 'something';
}else{
echo 'somthing else';
}
}
add_action('parse_query','test');
I just need one echo not anymore but it gave me 6,7 times
It’s not uncommon for actions to fire more than once. Surprising for “parse_query”, but beside the point. If your callback function is something that must only occur once per request, at the end of your function, have it remove itself from the call stack.
remove_action('parse_query', 'test');
thank you @bcworkz, it works