pv619
pv619

Reputation: 409

wordpress is_front_page() is not working (static front page)

I am using a WP - Alert plugin for a wordpress website. The issue is that it displays on all the pages even though it is set as display on home page only. (is_home). However, my home page is a static home page so I have set it to is_front_page and still it displays on all the pages. If you could just let me know if I am missing out anything??

function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
    $display    = true;
    $homeOption = get_option('alert_show_on_home');
    $pageOption = get_option('alert_show_on_page');
    $postOption = get_option('alert_show_on_post');

    $getID = get_the_ID();

    if($homeOption == 'yes' && is_home ()){
      $display =  true ;
    }elseif($pageOption == 'yes' && is_page()){
      $display =  true ;
    }elseif($postOption == 'yes' && is_single()){
      $display =  true ;
    }else{
      $display =  false ;
    }
    if($display){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>    
</div>
</div>
<?php
  }
}
}
?>

I added this line to the code above and still it won't display on the static home page only. } elseif($homeOption == 'yes' && is_front_page()){ $display = true ; }

Thanks in advance guys :)

Upvotes: 1

Views: 3117

Answers (2)

user1409909
user1409909

Reputation:

WordPress loads functions.php before the $wp_query object has been set up with the current page. is_front_page is a wrapper around around $wp_query->is_front_page(), and if the query hasn't been set up, it's always going to return false.

For more on the topic, see this question : https://wordpress.stackexchange.com/questions/41805/is-front-page-only-works-in-theme-file-and-does-not-work-in-functions-php

To use it in your functions.php, you will have to wrap it in an action that is after the $wp_query object is instantiated.

add_action('wp', function () {
    if (!is_front_page()) {
        add_action('wp_print_styles', function () {
            wp_deregister_style('wpos-slick-style');
            wp_deregister_style('wpsisac-public-style');
        }, 100);
    }
});

Upvotes: 3

wanderer
wanderer

Reputation: 111

Is this showing in your static home page now??

Try this

function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
    $homeOption = get_option('alert_show_on_home');
    $getID = get_the_ID();
    if($homeOption == 'yes' && is_home ()){
   ?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>    
</div>
</div>
<?php
  }
}
}
?>

Hope this works for you

Upvotes: 1

Related Questions