admin管理员组

文章数量:1289601

is_front_page() in the below code is not working... this function just doesn't run when I'm on the front page, any idea why? am I doing something wrong?

function save_landing_page_slider()
{
    
        if (isset($_GET['slider']) && is_front_page()) {
                $current_user_id = get_current_user_id();
                $slider = $_GET['slider'];
                update_field('user_landing_slider', $slider, 'user_'.$current_user_id);
        }
   
   
}
add_action('init', 'save_landing_page_slider', 10, 2);

is_front_page() in the below code is not working... this function just doesn't run when I'm on the front page, any idea why? am I doing something wrong?

function save_landing_page_slider()
{
    
        if (isset($_GET['slider']) && is_front_page()) {
                $current_user_id = get_current_user_id();
                $slider = $_GET['slider'];
                update_field('user_landing_slider', $slider, 'user_'.$current_user_id);
        }
   
   
}
add_action('init', 'save_landing_page_slider', 10, 2);
Share Improve this question asked Jul 12, 2021 at 0:49 user205498user205498 31 bronze badge 0
Add a comment  | 

1 Answer 1

Reset to default 1
add_action('init', 'save_landing_page_slider', 10, 2);

init is too early — if you look at the WordPress query overview on Codex, init is (as of writing) the second step, whereas is_ variables like $is_page (or WP_Query::$is_page) that are used by conditional tags like is_front_page() are only set in step 4, so instead of init, you should use a later hook like wp or template_redirect:

add_action( 'template_redirect', 'save_landing_page_slider' );

So in order for conditional tags to work properly, make sure to call them in the right hook or place.

Additionally, you should also understand when would is_front_page() return true, e.g. when your homepage is set to a static Page, and that you're on the homepage.

本文标签: isfrontpage is not working in my functionsphp