1

我正在为 wordpress 网站使用 WP - Alert 插件。问题是它显示在所有页面上,即使它设置为仅在主页上显示。(is_home)。但是,我的主页是一个静态主页,所以我将它设置为 is_front_page 并且它仍然显示在所有页面上。如果你能告诉我我是否遗漏了什么?

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
  }
}
}
?>

我在上面的代码中添加了这一行,但它仍然不会只显示在静态主页上。} elseif($homeOption == '是' && is_front_page()){ $display = true ; }

在此先感谢各位:)

4

2 回答 2

2

WordPress在当前页面设置对象 functions.php之前加载。是 around 的包装器,如果尚未设置查询,它总是会返回 false。$wp_queryis_front_page$wp_query->is_front_page()

有关该主题的更多信息,请参阅此问题:https ://wordpress.stackexchange.com/questions/41805/is-front-page-only-works-in-theme-file-and-does-not-work-in-函数-php

要在您functions.php的 .

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);
    }
});
于 2013-09-09T08:37:12.643 回答
1

这现在显示在您的静态主页中吗?

尝试这个

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
  }
}
}
?>

希望这对你有用

于 2013-09-09T08:41:44.037 回答